La sélection Multiple dans Spring MVC 3.0

Ok, donc j'ai essayé d'accomplir de multiples sélectionne dans Spring MVC pour un certain temps et n'ont eu aucune chance.

Fondamentalement, ce que j'ai est une Compétence de classe:

public class Skill {
    private Long id;
    private String name;
    private String description;
    //Getters and Setters
}

Et un Employé qui a des Compétences multiples:

public class Employee {
    private Long id;
    private String firstname;
    private String lastname;
    private Set<Skill> skills;
    //Getters and Setters
}

Tous ces éléments sont mappés à Hibernate, mais qui ne devrait pas être un problème.

Maintenant, je voudrais être en mesure de le faire dans la page JSP est de choisir les Compétences d'un Employé à partir d'un <select multiple="true"> élément.

J'ai essayé ceci dans la page JSP avec pas de chance:

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

Voici mon 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";
    }
}

Mais cela ne semble pas fonctionner. J'obtiens l'Exception suivante:

SEVERE: Servlet.service() for servlet jsp threw exception
javax.servlet.jsp.JspException: Type [java.lang.String] is not valid for option items

J'ai l'impression que ça devrait être possible là où nous pouvons avoir un formulaire pour un Modèle qui a de multiples sélectionnez à partir d'une liste d'options. Quelle est la meilleure pratique pour avoir form:select et form:options dans Spring MVC 3.0?

Merci!

Solution:

Ok, donc juste au cas où quelqu'un se demande ce que la solution est. En outre à l'utilisateur 01001111 correctif:

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

Nous avons besoin d'ajouter un CustomCollectionEditor au contrôleur comme suit:

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

Cela permet de Printemps, d'ajouter des Ensembles de Compétences entre les JSP et le Modèle.

InformationsquelleAutor Ian Dallas | 2010-12-02