좋아, 나는 잠시 동안 Spring MVC에서 여러 개의 선택을 수행하려고 노력했지만 행운이 없었다.다중 선택 (Spring 3.0) 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
}
.
이제는 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
을 갖는 가장 좋은 방법은 무엇입니까?
감사합니다.
솔루션 :
단지의 경우 사람이 솔루션이 무엇인지 궁금해 그래서 좋아.
이@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와 모델 사이의 스킬 세트를 추가 할 수 있습니다 : 사용자 01001111 수정하는 것 외에도 다음과 같이
<form:select multiple="true" path="skills">
<form:options items="${skillOptionList}" itemValue="name" itemLabel="name"/>
<form:select>
우리는 컨트롤러에 CustomCollectionEditor
를 추가해야합니다.
<form:select multiple="true" path="skills">
<form:options items="${skillOptionList}" itemValue="name" itemLabel="name"/>
</form:select>
대신 skillOptionList
이렇게 도움이되었습니다. 내 JSP에서 skillOptionList를 볼 수 있지만 save를 클릭하면 setSkills가 호출되지 않으며 기술이 내 Employee와 연결되지 않습니다. 어떤 아이디어? –
나는 그것을 이해했다. 원하는 경우 다른 사용자가 동일한 문제를 겪고있는 경우 내 게시물에 명시된대로 CustomCollectionEditor에 대한 부분을 솔루션에 추가하십시오. 감사! –