Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple Select in Spring 3.0 MVC
    primarykey
    data
    text
    <p>Ok so I've been trying to accomplish multiple selects in Spring MVC for a while and have had no luck.</p> <p>Basically what I have is a Skill class:</p> <pre><code>public class Skill { private Long id; private String name; private String description; //Getters and Setters } </code></pre> <p>And an Employee who has multiple Skills:</p> <pre><code>public class Employee { private Long id; private String firstname; private String lastname; private Set&lt;Skill&gt; skills; //Getters and Setters } </code></pre> <p>All of these are mapped to Hibernate but that shouldn't be an issue.</p> <p>Now I would like to be able to do in the JSP is to select Skills for an Employee from a <code>&lt;select multiple="true"&gt;</code> element. </p> <p>I have tried this in the JSP with no luck:</p> <pre><code>&lt;form:select multiple="true" path="skills"&gt; &lt;form:options items="skillOptionList" itemValue="name" itemLabel="name"/&gt; &lt;form:select&gt; </code></pre> <p>Here is my Controller:</p> <pre><code>@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&lt;String, Object&gt; map) { employeeService.addEmployee(emp); return "redirect:/indexEmployee.html"; } @RequestMapping("/indexEmployee") public String listEmployees(@RequestParam(required=false) Integer id, Map&lt;String, Object&gt; 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"; } } </code></pre> <p>But this does not seem to work. I get the following Exception:</p> <pre><code>SEVERE: Servlet.service() for servlet jsp threw exception javax.servlet.jsp.JspException: Type [java.lang.String] is not valid for option items </code></pre> <p>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 <code>form:select</code> and <code>form:options</code> in Spring 3.0 MVC?</p> <p>Thanks!</p> <p><strong>Solution:</strong></p> <p>Ok so just in case anyone wonders what the solution is. In addition to user 01001111 fix:</p> <pre><code>&lt;form:select multiple="true" path="skills"&gt; &lt;form:options items="${skillOptionList}" itemValue="name" itemLabel="name"/&gt; &lt;form:select&gt; </code></pre> <p>We need to add a <code>CustomCollectionEditor</code> to the controller as follows:</p> <pre><code>@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 &amp;&amp; !((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; } }); } } </code></pre> <p>This allows Spring to add Sets of Skills between the JSP and Model.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload