java - How to implement correct data binding in controller? -


i have jsp page combines user object using forms. on last form i'm trying collection<permission>. when i'm trying pass data 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 convert property value of type 'java.lang.string[]' required type 'java.util.collection' property 'permissions'; nested exception java.lang.illegalstateexception: cannot convert value of type [java.lang.string] required type [it.marco.javaproject.domain.permission] property 'permissions[0]': no matching editors or conversion strategy found] 

here 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 need use kind of data binder in controller. how implement it? how translate string[] collection of permission?

i find simple solution. here 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)));         }     }); } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -