开发者

Multiple Select in Spring 3.0 MVC

Ok so I've been trying to accomplish multiple selects in Spring MVC for a while and have had no luck.

Basically what I have is a Skill class:

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

And an Employee who has multiple Skills:

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

All of these are mapped to Hibernate but that shouldn't be an issue.

Now I would like to be able to do in the JSP is to select Skills for an Employee from a <select multiple="true"> element.

I have tried this in the JSP with no luck:

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

Here is my 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 li开发者_运维知识库stEmployees(@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";
    }
}

But this does not seem to work. I get the following Exception:

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

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 form:select and form:options in Spring 3.0 MVC?

Thanks!

Solution:

Ok so just in case anyone wonders what the solution is. In addition to user 01001111 fix:

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

We need to add a CustomCollectionEditor to the controller as follows:

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

This allows Spring to add Sets of Skills between the JSP and Model.


You need to treat the items attribute as a variable, not just reference the variable name:

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

put ${skillOptionList} instead of skillOptionList


You don't need the custom editors - this is all I do and it copies the values back and forth correctly:

<form:select path="project.resources">
    <form:option value="XX"/>
    <form:option value="YY"/>
</form:select>

Project class:-
private Set<String> resources;

This is how I add the data in the controller:

Set<String> resources3 = new HashSet<String>();
resources3.add("XX");


I found the above non working. In addition to the things mentioned below i also used the answer in: Spring select multiple tag and binding (simply put overide equals and hashcode). I have also changed initBinder based on the comment above

It took me a lot of time to cope with it so i thought to give a hint to anyone looking the same thing and having the problems i met with.


tkeE2036: I wonder how you said it worked for you at all? The value of each option is "name", not "id". But then in the convertElement method you treat the received element (which is a name), as if it were the id. My guess is that if you tried to set the value of each option as "id", you would get an empty string as the value of each option, because you are using the wrong PropertyEditor.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜