程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Spring 3.0 MVC中的多项选择大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Spring 3.0 MVC中的多项选择?

开发过程中遇到Spring 3.0 MVC中的多项选择的问题如何解决?下面主要结合日常开发的经验,给出你关于Spring 3.0 MVC中的多项选择的解决方法建议,希望对你解决Spring 3.0 MVC中的多项选择有所启发或帮助;

您需要将items属性视为一个变量,而不仅仅是引用变量名称:

<form:SELEct multiple="true" path="skills">
    <form:options items="${skillOptionList}" itemValue="name" itemLabel="name"/>
</form:SELEct>

${skillOptionList}而不是skillOptionList

解决方法

好的,所以一段时间以来我一直在尝试在Spring MVC中完成多项选择,但是没有运气。

基本上我有一个技能课:

public class Skill {
    private Long id;
    private String name;
    private String description;
    //Getters and Setters
}

拥有多种技能的员工:

public class employee {
    private Long id;
    private String firstname;
    private String lastname;
    private Set<Skill> skills;
    //Getters and Setters
}

所有这些都映射到了Hibernate,但这不应该成为问题。

现在,我希望能够在JSP中从<SELEct multiple="true">元素中选择雇员的技能。

我在JSP中尝试过此方法,但没有运气:

<form:SELEct multiple="true" path="skills">
    <form:options items="skillOptionList" itemValue="name" itemLabel="name"/>
<form:SELEct>

这是我的控制器:

@Controller
@SessionAttributes
public class employeeController {
     @Autowired
     private employeeservice service;

     @requestMapping(value="/addemployee",method = requestMethod.POST)
     public String addSkill(@modelAttribute("employee") employee emp,BindingResult result,Map<String,Object> map) {

        employeeservice.addemployee(emp);

        return "redirect:/indexemployee.html";
    }

    @requestMapping("/indexemployee")
    public String listemployees(@requestParam(required=falsE) Integer id,Object> map) {

        employee emp = (id == null ? new employee() : employeeservice.loadById(id));

        map.put("employee",emp);
        map.put("employeeList",employeeservice.listemployees());
        map.put("skillOptionList",skillservice.listSkills());

        return "emp";
    }
}

但这似乎不起作用。我收到以下异常:

SEVERE: Servlet.service() for servlet jsp threw exception
javax.servlet.jsp.JspException: Type [java.lang.String] is not valid for option items

我觉得应该可以在其中提供一个模型表单,该表单可以从提供的选项列表中进行多项选择。什么是有最佳实践form:SELEct,并form:options在Spring
MVC 3.0?

谢谢!

解:

好的,以防万一有人想知道解决方案是什么。除了用户01001111修复:

<form:SELEct multiple="true" path="skills">
    <form:options items="${skillOptionList}" itemValue="name" itemLabel="name"/>
<form:SELEct>

我们需要向CustomcatollectionEditor控制器添加a ,如下所示:

@Controller
@SessionAttributes
public class employeeController {

    @Autowired
    private employeeeservice employeeservice;

    @Autowired
    private Skillservice skillservice;

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Set.class,"skills",new CustomcatollectionEditor(Set.class)
          {
            @Override
            protected Object convertElement(Object element)
            {
                Long id = null;

                if(element instanceof String && !((String)element).equals("")){
                    //From the JSP 'element' will be a String
                    try{
                        id = Long.parseLong((String) element);
                    }
                    catch (numberFormatException E) {
                        System.out.println("Element was " + ((String) element));
                        e.printStackTrace();
                    }
                }
                else if(element instanceof Long) {
                    //From the database 'element' will be a Long
                    id = (Long) element;
                }

                return id != null ? employeeservice.loadSkillById(id) : null;
            }
          });
    }
}

这使Spring可以在JSP和模型之间添加技能集。

大佬总结

以上是大佬教程为你收集整理的Spring 3.0 MVC中的多项选择全部内容,希望文章能够帮你解决Spring 3.0 MVC中的多项选择所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。