Announcement Announcement Module
Collapse
No announcement yet.
Property Editor Spring MVC Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Property Editor Spring MVC

    i'm trying to create a listbox where the user will be able to selct multiply values. My view is this:


    View:
    <td>
    <form:label path="role">Role:</form:label></td>
    <td>
    <select name="role" multiple="true">
    <OPTION VALUE="admin">admin</OPTION>
    <OPTION VALUE="user">user</OPTION>
    </select>
    </td>

    Model:

    .
    .
    fields
    .
    private ArrayList<Role> role;

    get/set

    the Role class:

    public class Role {
    private String role;

    public String getRole() {
    return role;
    }

    public void setRole(String role) {
    this.role = role;
    }
    }

    So according to this post http://stackoverflow.com/questions/3...eckboxes-issue, i create a new class which is the following:

    public class RoleEditor extends PropertyEditorSupport {

    @Override
    public String getAsText() {
    return ((Role) getValue()).getRole();
    }

    @Override
    public void setAsText(String text) throws IllegalArgumentException {
    setValue(text);
    }
    }

    And finally in the controller class i add this:

    @InitBinder
    @RequestMapping(value = "/log", method = RequestMethod.POST)
    public String setRegistrationForm(@ModelAttribute("logAtr") Register register,
    Registration registration,Model model,HttpSession session,WebDataBinder b) {

    b.registerCustomEditor(Role.class, new RoleEditor());
    .
    .
    return "aView"
    }

    But i still take this exception: Cannot convert value of type [java.lang.String[]] to required type [java.util.ArrayList] for property 'roles': no matching editors or conversion strategy found]
    I try to follow the example which seems to be correct but the problem didn't solve. What i'm doing wrong?

  • #2
    According to the Spring documentation, controller methods annotated with @InitBinder do not support form/ command objects and do not return any value. You should try changing your code as follows:

    Code:
    @InitBinder
    public void initBinder(final WebBinder binder)
    {
        binder.registerCustomEditor(Role.class, new RoleEditor());
    }
    
    @RequestMapping(value = "/log", method = RequestMethod.POST)
    public String setRegistrationForm(@ModelAttribute("logAtr") Register register, Registration registration,Model model,HttpSession session)
    {
        ...
        return "aView";
    }

    Comment

    Working...
    X