Spring MVC - Populating domain object (Form backing Object) in view and updating some properties

Sometimes we need to update the domain object partially. We get complete object from database but we have to update some fields of it using html form. In this situation, original Data Object is kept on the server and some fields are asked to be filled by user.

In Sprint MVC, we have implemented feature that helps us to get it done. There is @ModelAttribute annotation that captures Bean object at server and maps its values to submitted form's fields.

In following code, pupulateContact() method annotated with @ModelAttribute annotation. This make sure that this method is called before any @RequestMapping annotated method like open() and save() in this code. pupulateContact() method returns an object having some fields initialized. We want to get other fields from HTML form. After submission ,Contact Object will have all values in it. It remembers the initialized value and get updated with new values those are sent in request. Other will remain as it is.

package pack;

import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class MyController {
    @ModelAttribute("contact")
    public Contact populateContact(){
        System.out.println("populateContact");
        Contact contact=new Contact();
        contact.setName("aaaaaa");
        contact.setPhone(35345444);
        return contact;
    }
    
    @RequestMapping(value = "open", method=RequestMethod.GET)
    public String viewContact() {
        return "f1";
    }
    
    @RequestMapping(value = "save", method=RequestMethod.POST)
    public String savContact(@ModelAttribute("contact") Contact contact,HttpServletRequest req) {
        System.out.println("Recieved values : \n"+contact);
        return "r1";
    }
}

Bean Class

package pack;

public class Contact {
    private int id;
    private String name;
    private String email;
    private long phone;

    public Contact() {
System.out.println("Created new Contact");
    }

    public Contact(int id, String name, String email, long phone) {
        super();
        this.id = id;
        this.name = name;
        this.email = email;
        this.phone = phone;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public long getPhone() {
        return phone;
    }

    public void setPhone(long phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "Contact [id=" + id + ", name=" + name + ", email=" + email
                + ", phone=" + phone + "]";
    }

}

JSP Form page


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>

</head>
<body>
    <form:form method="post" commandName="contact" action="save">
        Name : <form:input path="name" />
        <br />
        Email : <form:input path="email" />
        <br />
        <form:button>Submit</form:button>
    </form:form>
</body>
</html>

Download complete code

Understanding run time polymorphism of an object

Run-time polymorphism is the ability of an object to play multiple roles at run-time. See the following program where the object of Cal class will have different behaviors according the reference variable through which it is pointed. If we access Cal object using c, it has add() and sub() methods. If we access it using a reference variable then only add() method is accessible and same is for b variable that allows to access only sub() method.

So in spite of having same object, It is playing different role according to the type of reference variable that is pointing it.

//Example of polymorphism
package poly;
interface Adder{
    void add(int x,int y);
}
interface Substractor{
    void sub(int x,int y);
}
class Cal implements Adder,Substractor{
    @Override
    public void add(int a,int b){
        System.out.println(a+b);
    }
    @Override
    public void sub(int s,int y){
        System.out.println(s-y);
    }
}

public class PolyMorphicViewOfAnObject {
    public static void main(String[] args) {
        Cal c=new Cal();
        Adder a=c;
        Substractor s=c;

        s.sub(12,3);

        a.add(2334);
        
        c.add(23455);
        c.sub(445);
    }
}


Popular Posts