Spring JSP Checkboxes on List Object -


i trying use <checkboxes> tag on list object. despite reading mykong tutorial , searching elsewhere, can't figure out how done.

so here want do: have class like

 class person{      list<icecreams> creams;      } 

so want give user form can choose icecreams likes.

controller:

@controller public class icecreamcontroller{  @requestmapping(value="icecream", method=requestmethod.get) public string showpage(model model){ person person = repository.getperson(); //returns person, "creams" not empty model.addattribute("creams", person.geticecreams(); }  @requestmapping(value="icecream", method=requestmethod.post) public string showpage( @modelattribute("teilnehmer") list<icecreams> likedcreams, model model){ //do selected icecreams } 

now don't understand how continue in jsp. know have use checkboxes tag, not know returns on submit or if use correctly.

<form:form> <form:checkboxes path="creams" items="${creams}"/> <input type="submit" value="submit"> </form:form> 

so question is: write in jsp , returned controller?

added after comment: icecream class:

  public class icecream{    private long id;    private string creamname; 

//+getters/setters }


edit: after helpful answer tried this: adding model:

model.addattribute("person", person);      model.addattribute("creams", person.getcreams());  

and in jsp did

<form:checkboxes  path="teilnehmer"                       items="${creams}"                       itemvalue="id"                       itemlabel="creamname"                       /> 

so in post-method take modelattribute person.

added controller:

@initbinder protected void initbinder(httpservletrequest request, servletrequestdatabinder binder) throws exception {    binder.registercustomeditor(icecream.class, new icecreamspropertyeditor()); 

and new editor class:

public class contactspropertyeditor extends propertyeditorsupport{      @autowired     icecreamrepository creamrep;     @override    public void setastext(string text) throws illegalargumentexception {           integer creamid = new integer(text);          icecream cream = creamrep.findone(creamid);          super.setvalue(con);     } } 

sadly result error 400.

firstly, cannot bind raw list. need bind object wrapping list: in case instance of person rather list creams.

so, put person in model. use @modelattribute method framework reload same person on submit , set values. want present available ice creams selection.

@requestmapping(method=requestmethod.get) public string loadforedit(){     return ""; }  @requestmapping(method=requestmethod.post) public string save(@modelattribute("person") person person){     repository.saveperson(person);        return ""; }  //called framework on 'get' load person wish edit //called framework on on 'post' same instance binding //send personid hidden form element in form @modelattribute("person") public person getperson(@requestparam int personid){     return repository.getperson(personid);   }   @modelattribute("icecreams") public list<string> getavailableicecreams(){     return repository.findall();     } 

secondly, framework cannot automatically convert submitted form parameters instances of icecream. need @ using converter question. see here:

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html

given above can simpler example working changing collection type string:

class person{     list<string> creams; } 

the jsp should become:

<form:form modelattribute="person">     <!-- bind creams property of person -->     <!-- create check boxes available ice creams -->     <!-- in person.creams should automatically checked -->     <form:checkboxes path="creams" items="${icecreams}" />     <input type="hidden" value="${person.id}" name="personid"/>     <input type="submit" value="submit"> </form:form> 

once familiar converters can convert bind icecream instances broad topic. in jsp should need update checkboxes tag below:

<form:checkboxes path="creams" items="${icecreams}"  itemvalue="id" itemlabel="labelname"/> 

where value property submitted server , used converter create correct instance (e.g. id of items saved in database) , label property used display.


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -