Spring 3.0 MVC中的多选

Ian*_*las 26 select spring jsp spring-mvc

好的,所以我一直试图在Spring MVC中完成多次选择并且没有运气.

基本上我所拥有的是技能课程:

public class Skill {
    private Long id;
    private String name;
    private String description;
    //Getters and Setters
}
Run Code Online (Sandbox Code Playgroud)

拥有多项技能的员工:

public class Employee {
    private Long id;
    private String firstname;
    private String lastname;
    private Set<Skill> skills;
    //Getters and Setters
}
Run Code Online (Sandbox Code Playgroud)

所有这些都映射到Hibernate但这应该不是问题.

现在我希望能够在JSP中为<select multiple="true">元素中的Employee选择Skills .

我在JSP中试过没有运气:

<form:select multiple="true" path="skills">
    <form:options items="skillOptionList" itemValue="name" itemLabel="name"/>
<form:select>
Run Code Online (Sandbox Code Playgroud)

这是我的控制器:

@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, Map<String, 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";
    }
}
Run Code Online (Sandbox Code Playgroud)

但这似乎不起作用.我得到以下例外:

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

我觉得应该有可能我们可以为一个模型提供一个表单,该模型可以从提供的选项列表中进行多项选择.什么是有最佳实践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>
Run Code Online (Sandbox Code Playgroud)

我们需要CustomCollectionEditor按如下方式向控制器添加一个:

@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 CustomCollectionEditor(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;
            }
          });
    }
}
Run Code Online (Sandbox Code Playgroud)

这允许Spring在JSP和Model之间添加一组技能.

小智 16

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

<form:select multiple="true" path="skills">
    <form:options items="${skillOptionList}" itemValue="name" itemLabel="name"/>
</form:select>
Run Code Online (Sandbox Code Playgroud)

${skillOptionList}替代skillOptionList

  • 我想到了.如果您愿意,请将您的CustomCollectionEditor部分添加到您的解决方案中,如我的帖子中所述,以防其他人遇到同样的问题.谢谢! (2认同)