gpt4 book ai didi

spring - Spring 3.0 MVC 中的多选

转载 作者:IT老高 更新时间:2023-10-28 13:47:15 36 4
gpt4 key购买 nike

好吧,我一直在尝试在 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 :

@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";
}
}

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

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 3.0 MVC 中?

谢谢!

解决方案:

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

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

我们需要添加一个 CustomCollectionEditor到 Controller 如下:

@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 和模型之间添加技能集。

最佳答案

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

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

${skillOptionList} 代替 skillOptionList

关于spring - Spring 3.0 MVC 中的多选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4331532/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com