2

I have a jsp page which combines User object using forms. On the last form I'm trying to get Collection<Permission>. But when I'm trying to pass data to controller I'm getting 400 Error because of:

Field error in object 'user' on field 'permissions':
rejected value [add,view];
codes [typeMismatch.user.permissions,typeMismatch.permissions,typeMismatch.java.util.Collection,typeMismatch];
arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.permissions,permissions];
arguments [];
default message [permissions]];
default message [Failed to convert property value of type 'java.lang.String[]' to required type 'java.util.Collection' for property 'permissions'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [it.marco.javaproject.domain.Permission] for property 'permissions[0]': no matching editors or conversion strategy found]

Here is my jsp form:

<form:form action="/user/permission" method="POST" modelAttribute="user">
   <form:checkboxes path="permissions" items="${permissions}" delimiter=<br>"/>
   <form:hidden path="email"/>
   <form:hidden path="password"/>
   <form:hidden path="name"/>
   <input type="submit" value="Next" name="next"/>
</form:form>

Part of controller:

public String processRoleForm(@ModelAttribute("user") User user, ModelMap model) {
    model.addAttribute("permissions", userService.getPermissions());
    return "user/form/permissionForm";
}

Permission class:

@Entity
@Table(name = "permission")
public class Permission implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @Column(name = "name")
    private String name;

If I'm not mistaken I need to use some kind of data binder in my controller. How to implement it? How to properly translate String[] to Collection of Permission?

1 Answer 1

1

I find simple solution. Here it is:

@InitBinder
public void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(Permission.class, new PropertyEditorSupport() {
        @Override
        public void setAsText(String id) throws IllegalArgumentException {
            setValue(userService.getPermission(Integer.parseInt(id)));
        }
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

This would be my preferred way, but you could have also referenced each permission individually by indexing them like path="permissions[0].value". However, doing that would mean that you would need to do a LOT of cleanup on the backend to "clean" unchecked items. I'd say your are WAY better off writing a proper binding for it and let it be handled that way so you get a complete list without having to do cleanup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.