"Spring MVC"-@ModelAttribute use

Source: Internet
Author: User

@ModelAttribute one has the following three functions:

① bind request parameter to command object: When placed on the parameter of a function processing method, it is used to bind multiple request parameters to a command object, thus simplifying the binding

and automatically exposed to model data for use in view page presentations;

② exposing a form reference object to model data: when placed on a processor's general method (non-functional processing method), prepares the form reference to be presented for the form

object, such as the city in which you want to select when registering, and automatically add a function before you perform a functional processing method (@RequestMapping annotation)

To the model object for use when the view page is displayed;

③ Exposure @requestmapping method returns a value of model data: when placed on the return value of a function processing method, the return value of the exposed function processing method is

Model data for use when the view page is displayed.

In spring MVC, @ModelAttribute are typically used in parameter annotations of the Controller method to interpret the model entity,

But at the same time, it can be placed in the method annotations.

If @modelattribute is placed on the annotation of a method, it is represented that all methods of the controller execute this @modelattribute method before the call

One: @ModelAttribute the parameter annotations applied to the Controller method

= "Application")publicclass  applicationcontroller{    public String Test1 (@ModelAttribute ("user") Usermodel user) {        }}

Two: @ModelAttribute put on Method " all methods of the controller execute this @modelattribute method before calling .

= "Application")publicclass  applicationcontroller{    @ModelAttribute (  "BasePath")    public  String basepath (httpservletrequest request) {        = Request.getcontextpath ();                         return Basepath.endswith ("/")? Basepath.substring (0, Basepath.length ()-1): BasePath;    }}

"Spring MVC"-@ModelAttribute use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.