The method that is annotated by @modelattribute is executed before each method of the controller is executed, so use it sparingly for the use of a controller to map multiple URLs
[email protected] Comment void return value method
@Controller public class Hellomodelcontroller { @ModelAttribute public void Populatemodel (@RequestParam String ABC, model model) { Model.addattribute ("AttributeName", ABC); } @RequestMapping (value = "/helloworld") public String HelloWorld () { return "helloworld.jsp"; } }
When accessing the HelloWorld method, the Populatemodel method is called first, and the page parameter ABC (/HELLOWORLD.HTML?ABC=TEXT) is placed in the AttributeName property of the model, which can be accessed directly in the view.
The JSP page page is as follows:
<body>
<c:out value="${attributename}"></c:out>
</body>
[Email protected] comments return a method of a specific class
@Controller public class Hello2modelcontroller { @ModelAttribute public User Populatemodel () { user User=new User (); User.setaccount ("Ray"); return user; } @RequestMapping (value = "/helloworld2") public String HelloWorld () { return "helloworld.jsp"; } }
When http://localhost:8080/test/helloWorld2.html is requested, the method is Populatemodel, the user object is returned, the name of the model property is not specified,
It is implicitly represented by the return type, and as this method returns the user type, the name of the model property is user.
<c:out value="${user.account}"></c:out>
You can also specify the property name
@Controller public class Hello2modelcontroller { @ModelAttribute (value= "MyUser") public User Populatemodel () { user user=new User (); User.setaccount ("Ray"); return user; } @RequestMapping (value = "/helloworld2") public String HelloWorld (Model map) { return "helloworld.jsp"; } }
<c:out value="${myuser.account}"></c:out>
Object merging:
@Controller public class Hello2modelcontroller { @ModelAttribute public User Populatemodel () { User User=new User (); User.setaccount ("Ray"); return user; } @RequestMapping (value = "/helloworld2") public String helloWorld (user user) { user.setname ("Lao Wang"); return "helloworld.jsp"; } }
Object Merge Specify Object name:
@Controller public class Hello2modelcontroller { @ModelAttribute ("MyUser") public User Populatemodel () { User User=new User (); User.setaccount ("Ray"); return user; } @RequestMapping (value = "/helloworld2") public String HelloWorld (@ModelAttribute ("MyUser") user user) { User.setname ("Lao Wang"); return "helloworld.jsp"; } }
This can be accessed in the JSP using the following methods
<c:out value="${myuser.name}"></c:out>
<c:out value="${myuser.account}"></c:out>
3. Control permissions through this attribute.
We can write this annotation in the base class method, the controller that needs to control the permission, inherit the controller to be able.
public class Basecontroller { @ModelAttribute public void Populatemodel () throws Exception { Sysuser user= Contextutil.getcurrentuser (); if (User.getaccount (). Equals ("admin")) { throw new Exception ("No Permissions");}}
@Controller public class Hello2modelcontroller extends Basecontroller { @RequestMapping (value = "/helloworld2 ") Public String HelloWorld (@ModelAttribute (" MyUser ") user user) { user.setname (" Lao Wang "); return "helloworld.jsp"; } }
There are many ways to control permissions, such as through filters, etc.
Summarize:
The @ModelAttribute has the following three functions:
① bind request parameters to a Command object: When placed on the parameter of a function processing method, it is used to bind multiple request parameters to a command object.
This simplifies the binding process and automatically exposes the model data for use when the view page is displayed.
In fact @modelattribute here is similar to Model.addattribute ("AttributeName", ABC) for the view page display.
Public String Test (@ModelAttribute ("user") Usermodel user)
Here is an annotated @modelattribute ("user"), whose purpose is to add the bound command object to the model object with the name "user" for use in the View page display.
We can now use ${user.username} on the view page to get the properties of the bound Command object.
② exposes the @requestmapping method return value to model data:
When placed on the return value of a function processing method, the return value of the exposed function processing method is the model data that is used when the view page is displayed.
Public @ModelAttribute ("user2") Usermodel test3 (@ModelAttribute ("user2") Usermodel user)
You can see that the return value type is a command object type, and through @modelattribute ("user2") annotations, the return value is exposed to the model data (named User2) for use in the view display
The return value of the @ModelAttribute annotation overrides the command object with the same name as the @modelattribute annotation in the @requestmapping annotation method
③ exposes the form reference object as model data:
When placed on a processor's general method (non-functional processing method), it is the form reference object that prepares the form for presentation, such as the city in which the registration is to be selected,
And it is automatically added to the model object before the function processing method (@RequestMapping annotation method) is used when the view page is displayed;
[email protected] annotations