Friday, 17 April 2015

Dropdown access nested child object of entity object in spring form:select box

If you want to access nested objects which is coming child object like if we have relationship of ManyToOne or OneToMany then when we fetch object of one entity then the dependent object's reference will also come inside that object
Lets take an Example.

We have to models as below 


public class Address implements Serializable {

    private static final long serialVersionUID = 1L;
    private int id;
    private String address1;
    private String address2;
    private String city;
    private String state;
    private String zip;
    private List<Contact> contacts;
                                     //all setter and getter here


public class Contact implements Serializable  {
    private static final long serialVersionUID = 1L;
    private Integer id;
    private String firstname;

    private String lastname;

    private String email;
    private String telephone;
    private Address address;
                                    //all setter and getter here

Now here we have used ManyToOne and OneToMany dependency.
so we can have more then one contact for one address.

Now if i am inserting a new contact then i have to select the address to which contact belong.
and when i get contact then related address should also get selected.
now lets take one controller which will have method to add and get the contact.


public class ContactController {

    private ContactService contactService;    
    private AddressService addressService;

    public String listContacts(Map<String, Object> map) {

            map.put("contact", contactService.listContact().get(0));
            map.put("contact", new Contact());

        map.put("addressList", addressService.listAddress());
        map.put("contactList", contactService.listContact());
        return "contact";

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addContact(@ModelAttribute("contact")
    Contact contact, BindingResult result) {

        return "redirect:/index";

    public String deleteContact(@PathVariable("contactId")
    Integer contactId) {


        return "redirect:/index";

Now lets create the contact.jsp page on which we will have a form to add the contact and display first contact filled in form if any.


 <%@taglib uri="" prefix="spring"%>
<%@taglib uri="" prefix="form"%>
<%@taglib uri="" prefix="c"%>
<title>Spring 3 MVC Series - Contact Manager</title>

    <h2>Contact Manager</h2>

    <form:form method="post" action="add.html" commandName="contact">

                <td>First Name</td>
                <td><form:input path="firstname" /></td>
                <td>Last Name</td>
                <td><form:input path="lastname" /></td>
                <td><form:input path="email" /></td>
                <td><form:input path="telephone" /></td>
               <c:if test="${!empty addressList}">
                        <form:select path="" itemValue="">
                                <c:forEach items="${addressList}" var="address">

                                    <form:option value="${}">${address.address1},${address.address2},${}</form:option>

                <td colspan="2"><input type="submit"
                    value="<spring:message code="label.addcontact"/>" /></td>

    <c:if test="${!empty contactList}">
        <table class="data">
                <th>Name</th>          <th>Email</th>           <th>Telephone</th>          <th>&nbsp;</th>
            <c:forEach items="${contactList}" var="contact">
                    <td>${contact.lastname}, ${contact.firstname}</td>
                    <td><a href="delete/${}">delete</a></td>


This will diasplay the form which is allow to add the contact to selected address and display the contact with its related address seleted in <form:select> drop down. 

NOTE: Create required serveries and dao.