だから私はしばらくの間、複数の選択をSpring MVCで達成しようとしていて、運がなかった。これらのすべては、HibernateにマッピングされているSpring 3.0で複数選択MVC
public class Employee {
private Long id;
private String firstname;
private String lastname;
private Set<Skill> skills;
//Getters and Setters
}
が、それは問題ではありません。
public class Skill {
private Long id;
private String name;
private String description;
//Getters and Setters
}
と複数のスキルを持っている従業員:
基本的に私が持っているものは、スキルクラスであります。
今私は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, 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
私たちが提供するオプションのリストから複数選択を持っているモデルのためのフォームを持つことができる場所、それは可能なはずのように私は感じます。 Spring 3.0 MVCでform:select
とform:options
を持つベストプラクティスは何ですか?
ありがとうございます!
ソリューション:
念のために誰もが解決策とは何か不思議ので、[OK]をクリックします。ユーザー01001111修正に加えて:
<form:select multiple="true" path="skills">
<form:options items="${skillOptionList}" itemValue="name" itemLabel="name"/>
<form:select>
我々は次のように、コントローラに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;
}
});
}
}
これは、春にはJSPとモデルの間でスキルのセットを追加することができます。
<form:select multiple="true" path="skills">
<form:options items="${skillOptionList}" itemValue="name" itemLabel="name"/>
</form:select>
ではなくskillOptionList
そのように助けられました。 JSPでskillOptionListが表示されるようになりましたが、saveをクリックしてもsetSkillsは呼び出されず、Employeeにスキルは関連付けられません。何か案は? –
私はそれを理解しました。あなたが望むなら、他の誰かが同じ問題を抱えている場合の私の投稿に記載されているように、あなたのソリューションにCustomCollectionEditorについての部分を追加してください。ありがとう! –