What is @ModelAttribute in Spring MVC?

后端 未结 13 880
一个人的身影
一个人的身影 2020-11-22 08:43

What is the purpose and usage of @ModelAttribute in Spring MVC?

13条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 09:44

    I know I am late to the party, but I'll quote like they say, "better be late than never". So let us get going, Everybody has their own ways to explain things, let me try to sum it up and simple it up for you in a few steps with an example; Suppose you have a simple form, form.jsp

    
    First Name :  
    

    Last Name :

    path="firstName" path="lastName" These are the fields/properties in the StudentClass when the form is called their getters are called but once submitted their setters are called and their values are set in the bean that was indicated in the modelAttribute="student" in the form tag.

    We have StudentController that includes the following methods;

    @RequestMapping("/showForm")
    public String showForm(Model theModel){ //Model is used to pass data between 
    //controllers and views
        theModel.addAttribute("student", new Student()); //attribute name, value
    return "form";
    }
    
    @RequestMapping("/processForm")
    public String processForm(@ModelAttribute("student") Student theStudent){
        System.out.println("theStudent :"+ theStudent.getLastName());
    return "form-details";
    }
    
    //@ModelAttribute("student") Student theStudent
    //Spring automatically populates the object data with form data all behind the 
    //scenes 
    

    now finally we have a form-details.jsp

    Student Information
    ${student.firstName}
    ${student.lastName}
    

    So back to the question What is @ModelAttribute in Spring MVC? A sample definition from the source for you, http://www.baeldung.com/spring-mvc-and-the-modelattribute-annotation The @ModelAttribute is an annotation that binds a method parameter or method return value to a named model attribute and then exposes it to a web view.

    What actually happens is it gets all the values of your form those were submitted by it and then holds them for you to bind or assign them to the object. It works same like the @RequestParameter where we only get a parameter and assign the value to some field. Only difference is @ModelAttribute holds all form data rather than a single parameter. It creates a bean for you that holds form submitted data to be used by the developer later on.

    To recap the whole thing. Step 1 : A request is sent and our method showForm runs and a model, a temporary bean is set with the name student is forwarded to the form. theModel.addAttribute("student", new Student());

    Step 2 : modelAttribute="student" on form submission model changes the student and now it holds all parameters of the form

    Step 3 : @ModelAttribute("student") Student theStudent We fetch the values being hold by @ModelAttribute and assign the whole bean/object to Student.

    Step 4 : And then we use it as we bid, just like showing it on the page etc like I did

    I hope it helps you to understand the concept. Thanks

提交回复
热议问题