The method annotated by @ modelattribute is executed before each method of the controller is executed. Therefore, you must be cautious when using a controller to map multiple URLs.
When writing the Controller code, we separate the Save method into a controller.
[Email protected] comment the void Return Value Method
@Controllerpublic class HelloModelController { @ModelAttribute public void populateModel(@RequestParam String abc, Model model) { model.addAttribute("attributeName", abc); } @RequestMapping(value = "/helloWorld") public String helloWorld() { return "helloWorld.jsp"; } }
In this Code, when accessing the Controller Method helloworld, The populatemodel method is called first, and the page parameter abc (/helloworld. ht? ABC = text) in the attributename attribute of the model, which can be directly accessed in the view.
The JSP page is as follows.
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
[Email protected] comment the method of returning a specific class
@Controllerpublic 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 a user requests http: // localhost: 8080/test/helloworld2.ht, first access the populatemodel method and return the user object. The model attribute name is not specified,
It is implicitly indicated by the return type. If this method returns the user type, the name of this model attribute is user.
In this example, the model property name has an implicit representation of the returned object type, and the model property object is the return value of the method. It does not require specific parameters.
JSP:
<c:out value="${user.account}"></c:out>
You can also specify the attribute name.
@Controllerpublic 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"; } }
JSP:
<c:out value="${myUser.account}"></c:out>
Object merging:
@ Controllerpublic class hello2modelcontroller {@ modelattribute public user populatemodel () {user = new user (); User. setaccount ("Ray"); Return user;} @ requestmapping (value = "/helloworld2") Public String helloworld (User user) {user. setname ("Old Wang"); Return "helloworld. JSP ";}}
This is useful when writing code. For example, during the update process, we can get the object according to the ID in the populatemodel method, and then use the automatic assembly function of spring MVC to assemble the object.
User object, so that the property of the value submitted on the client will be assembled into the object.
For example, the user object first obtains this object from the database. The client form only has the account attribute, and only the account attribute will be changed when submitted.
Name of the specified object for object merging:
@ Controllerpublic class hello2modelcontroller {@ modelattribute ("myuser") public user populatemodel () {user = new user (); User. setaccount ("Ray"); Return user;} @ requestmapping (value = "/helloworld2") Public String helloworld (@ modelattribute ("myuser") User user) {user. setname ("Old Wang"); Return "helloworld. JSP ";}}In this way, you can access
<c:out value="${myUser.name}"></c:out><c:out value="${myUser.account}"></c:out>
3. Use this feature to control permissions.
We can control the write annotation in the base class method. The controller that needs to control the permission can inherit the controller.
Public class basecontroller {@ modelattribute public void populatemodel () throws exception {sysuser user = contextutil. getcurrentuser (); If (user. getaccount (). equals ("admin") {Throw new exception ("no permission ");}}}
The class to be controlled inherits the basecontroller
@ Controllerpublic class hello2modelcontroller extends basecontroller {@ requestmapping (value = "/helloworld2") Public String helloworld (@ modelattribute ("myuser") User user) {user. setname ("Old Wang"); Return "helloworld. JSP ";}}In this way, you can control permissions. Of course, there are many methods to control permissions, such as using filters. Here is just a way of thinking.
@ Modelattribute usage