java - Binding nested objects in SPRING MVC -
i new spring mvc. not getting details of employee contains phone , address object well. actually, object not being binded employee.
follow code:
//(controller)
employeecontroller.java
@controller @requestmapping(value = "/employee") public class employeecontroller { @autowired employeeservice employeeservice; @requestmapping("/employee") public modelandview registeremployeer(@modelattribute employee employee) { map<string, object> modelmap = new hashmap<>(); modelmap.put("employee", new employee()); return new modelandview("employee", modelmap); } @requestmapping("/add") public string addemployee(@modelattribute employee employee) { employeeservice.save(employee); return "result"; } @modelattribute("phonetypelist") public map<string,string> populatephonetypelist() { return phone.getphonetypes(); } @modelattribute("addresstypelist") public map<string,string> populateaddresstypelist() { return address.getaddresstypes(); } }
//form takes employee details employee.jsp
<h2>employee information</h2> <form:form method="post" action="employee/add" modelattribute="employee"> <table> <tr> <td><form:label path="ssn">ssn</form:label></td> <td><form:input path="ssn" /></td> </tr> <tr> <td><form:label path="firstname">first name</form:label> </td> <td><form:input path="firstname" /></td> </tr> <tr> <td><form:label path="lastname">last name</form:label></td> <td><form:input path="lastname" /></td> </tr> <tr> <td><form:label path="dob">date of birth</form:label></td> <td><form:input path="dob" /></td> </tr> <tr> <td><form:label path="emailid">email</form:label></td> <td><form:input path="emailid" /></td> </tr> <tr> <td>phone type:</td> <td><form:select path="phonelist[0].phonetype" multiple="false"> <form:option value="none" label="--- select ---" /> <form:options items="${phonetypelist}" /> </form:select></td> </tr> <tr> <td></td> </tr> <tr> <td></td> </tr> <tr> <td><form:label path="phonelist[0].phoneno">phone</form:label></td> <td><form:input path="phonelist[0].phoneno" /></td> </tr> <tr> <td>address type:</td> <td><form:select path="addresslist[0].addresstype" multiple="false"> <form:option value="none" label="--- select ---" /> <form:options items="${addresstypelist}" /> </form:select></td> <tr> <td><form:label path="addresslist[0].street">street</form:label></td> <td><form:input path="addresslist[0].street" /></td> </tr> <tr> <td><form:label path="addresslist[0].city">city</form:label></td> <td><form:input path="addresslist[0].city" /></td> </tr> <tr> <td><form:label path="addresslist[0].state">state</form:label></td> <td><form:input path="addresslist[0].state" /></td> </tr> <tr> <td><form:label path="addresslist[0].zip">zip</form:label></td> <td><form:input path="addresslist[0].zip" /></td> </tr> <tr> <td><form:label path="addresslist[0].country">country</form:label></td> <td><form:input path="addresslist[0].country" /></td> </tr> <tr> <td></td> </tr> <tr> <td></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="submit" /></td> </tr> </table> </form:form>
//result after submitting form result.jsp
<h2>submitted employee information</h2> <table> <tr> <td>ssn</td> <td>${employee.ssn}</td> </tr> <tr> <td>first name</td> <td>${employee.firstname}</td> </tr> <tr> <td>last name</td> <td>${employee.lastname}</td> </tr> <tr> <td>date of birth</td> <td>${employee.dob}</td> </tr> <tr> <td>email</td> <td>${employee.emailid}</td> </tr> <tr> <td>phone type</td> <td>${employee.phonelist[0].phonetype}</td> </tr> <tr> <td>phone</td> <td>${phonelist[0].phoneno}</td> </tr> <tr> <td>address type</td> <td>${addresslist.addresstypelist}</td> </tr> <tr> <td>street</td> <td>${addresslist.street}</td> </tr> <tr> <td>city</td> <td>${addresslist.city}</td> </tr> <tr> <td>state</td> <td>${addresslist.state}</td> </tr> <tr> <td>zip</td> <td>${addresslist.zip}</td> </tr> <tr> <td>country</td> <td>${addresslist.country}</td> </tr> </table>
//my employee bean looks employee.java
public class employee { private list<phone> phonelist = new arraylist<phone>(); private list<address> addresslist = new arraylist<address>(); private long ssn; private string firstname; private string lastname; private string dob; private string emailid; public long getssn() { return ssn; } public void setssn(long ssn) { this.ssn = ssn; } public string getfirstname() { return firstname; } public void setfirstname(string firstname) { this.firstname = firstname; } public string getlastname() { return lastname; } public void setlastname(string lastname) { this.lastname = lastname; } public string getdob() { return dob; } public void setdob(string dob) { this.dob = dob; } public string getemailid() { return emailid; } public void setemailid(string emailid) { this.emailid = emailid; } public void addphone(phone ph) { phonelist.add(ph); } public void addaddress(address ad) { addresslist.add(ad); } public void setphonelist(list<phone> phonelist) { this.phonelist = phonelist; } public list<phone> getphonelist() { return phonelist; } public list<address> getaddresslist() { return addresslist; } public void setaddresslist(list<address> adlist) { this.addresslist = adlist; } }
i can details of employee ssn, first name, last name etc not phone type, phone no, address details field of objects. assumption binding not working here. idea?
i had similar problem, solved removing line:
@onetomany(cascade = cascadetype.all, orphanremoval = true)
before @elementcollection
not copied.
Comments
Post a Comment