Spring 3.0 MVC中的多项选择 [英] Multiple Select in Spring 3.0 MVC
问题描述
好的,所以我一直试图在Spring MVC中完成多项选择,但是没有运气.
Ok so I've been trying to accomplish multiple selects in Spring MVC for a while and have had no luck.
我基本上是一个技能班:
Basically what I have is a Skill class:
public class Skill {
private Long id;
private String name;
private String description;
//Getters and Setters
}
以及具有多种技能的员工:
And an Employee who has multiple Skills:
public class Employee {
private Long id;
private String firstname;
private String lastname;
private Set<Skill> skills;
//Getters and Setters
}
所有这些都映射到了Hibernate,但这不应该成为问题.
All of these are mapped to Hibernate but that shouldn't be an issue.
现在,我希望能够在JSP中从<select multiple="true">
元素中选择雇员的技能.
Now I would like to be able to do in the JSP is to select Skills for an Employee from a <select multiple="true">
element.
我在JSP中尝试过运气不好,
I have tried this in the JSP with no luck:
<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, 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";
}
}
但这似乎不起作用.我收到以下异常:
But this does not seem to work. I get the following Exception:
SEVERE: Servlet.service() for servlet jsp threw exception
javax.servlet.jsp.JspException: Type [java.lang.String] is not valid for option items
我觉得应该有可能为我们提供一个模型表格,该表格可以从提供的选项列表中进行多项选择.在Spring 3.0 MVC中使用form:select
和form:options
的最佳实践是什么?
I feel like it should be possible where we can have a form for a Model that has multiple select from a list of options provided. What is the best practice to have form:select
and form:options
in Spring 3.0 MVC?
谢谢!
解决方案:
好吧,以防万一有人想知道解决方案是什么.除了用户01001111修复:
Ok so just in case anyone wonders what the solution is. In addition to user 01001111 fix:
<form:select multiple="true" path="skills">
<form:options items="${skillOptionList}" itemValue="name" itemLabel="name"/>
<form:select>
我们需要向控制器添加CustomCollectionEditor
,如下所示:
We need to add a CustomCollectionEditor
to the controller as follows:
@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;
}
});
}
}
这允许Spring在JSP和模型之间添加技能集.
This allows Spring to add Sets of Skills between the JSP and Model.
推荐答案
您需要将items属性视为变量,而不仅仅是引用变量名:
You need to treat the items attribute as a variable, not just reference the variable name:
<form:select multiple="true" path="skills">
<form:options items="${skillOptionList}" itemValue="name" itemLabel="name"/>
</form:select>
放入${skillOptionList}
而不是skillOptionList
这篇关于Spring 3.0 MVC中的多项选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!