java - Unable to bind spring form checkbox -


i trying display full name, along checkboxes supervisor , manager. these 2 checkboxes checked if person has values these true.

what trying display users full name , supervior , manager columns having checkboxes , these checked or unchecked, , when form submitted changes binded well. tried model attribute don't have luck.

here's have far.

controller

@controller @requestmapping("/person.html") public class personscontroller {      @autowired     private personservice personservice;      @requestmapping(method = requestmethod.get)     public string initform(@modelattribute("personview")personview personview, bindingresult result, model model) {           list<person>  persons= personservice.getpersonlist();          personview persondata = new personview();         personview.setpersonlist(persons);           model.addattribute("personview", persondata);             return "member";     }      @requestmapping(method = requestmethod.post)     public string submitform(model model, personview personview, bindingresult result) {         system.out.println("controller runs");         model.addattribute("persons", personview);         return "successmember";     }  } 

view

public class personview {      private boolean issupervisor;     private boolean ismanager;     private list<person> personlist;       public boolean issupervisor() {         return issupervisor;     }      public void setsupervisor(boolean issupervisor) {         this.issupervisor = issupervisor;     }      public boolean ismanager() {         return ismanager;     }      public void setmanager(boolean ismanager) {         this.ismanager = ismanager;     }      public list<person> getpersonlist() {         return personlist;     }      public void setpersonlist(list<person> personlist) {         this.personlist = personlist;     }  } 

domain

public class person {    private string fullname;    private boolean issupervisor;   private boolean ismanager;  public string getfullname() {     return fullname; }  public void setfullname(string fullname) {     this.fullname = fullname; }  public boolean getissupervisor() {     return issupervisor; }  public void setissupervisor(boolean issupervisor) {     this.issupervisor = issupervisor; }  public boolean getismanager() {     return ismanager; }  public void setismanager(boolean ismanager) {     this.ismanager = ismanager; }  } 

jsp

        <html>         <title>persons information</title>         </head>          <body>              <form:form method="post" modelattribute="personview">                 <table>                     <tr>                         <th>full name</th>                         <th>supervisor</th>                         <th>manager</th>                     </tr>                      <c:foreach var="person" items="${personview.personlist}"                         varstatus="row">                         <tr>                             <td>{person.fullname}</td>                             <td><form:checkbox path="issupervisor"                                     value="${person.issupervisor}" /></td>                             <td><form:checkbox path="ismanager"                                     value="${person.ismanager}" /></td>                         </tr>                     </c:foreach>                       <tr>                         <td><input type="submit" name="submit" value="submit">                         </td>                     </tr>                     <tr>                 </table>             </form:form>          </body>         </html> 

any suggestion

the problem here need bind parameters corresponding person in list. if @ generated html see name attributes of checkboxes same i.e. on form submit there no way of determining parameter applies person.

as cannot bind raw list need keep wrapper: needs below:

public class personview {      private list<person> personlist;      public list<person> getpersonlist() {         return personlist;     }      public void setpersonlist(list<person> personlist) {         this.personlist = personlist;     } } 

person minor adjustments:

public class person {      private string fullname;      private boolean issupervisor;     private boolean ismanager;      public string getfullname() {         return fullname;     }      public void setfullname(string fullname) {         this.fullname = fullname;     }      public boolean issupervisor() {         return issupervisor;     }      public void setsupervisor(boolean issupervisor) {         this.issupervisor = issupervisor;     }      public boolean ismanager() {         return ismanager;     }      public void setmanager(boolean ismanager) {         this.ismanager = ismanager;     } } 

your jsp loop needs below:

<c:foreach var="person" items="${personview.personlist}" varstatus="row">     <tr>         <td>{person.fullname}</td>         <td><form:checkbox path="personlist[row.index].supervisor"                 value="true" /></td>         <td><form:checkbox path="personlist[row.index].manager"                 value="true" /></td>     </tr> </c:foreach> 

your controller:

@controller @requestmapping("/person.html") public class personscontroller {      @autowired     private personservice personservice;      @requestmapping(method = requestmethod.get)     public string initform() {         return "member";     }      @requestmapping(method = requestmethod.post)     public string submitform(@modelattribute personview personview) {         model.addattribute("persons", personview);         return "successmember";     }      @modelattribute     public personview getpersonview(){         list<person>  persons= personservice.getpersonlist();         personview pv = new personview();         pv.setpersonlist(persons);          return pv;     } } 

**all depends on order of list in form handler being same loaded rendering form , no people having been added in mean time.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -