Spring MVC @ ModelAttribute
1. @ ModelAttribute comment on 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"%>
2. @ ModelAttribute annotation returns the method of 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.html, first access the populateModel method and return the User object. The model attribute name is not specified and is implicitly indicated by the return type, if this method returns the User type, the model attribute name 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:
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:
Object merging:
@ Controllerpublic class Hello2ModelController {@ ModelAttribute public User populateModel () {User user User = new user (); User. setAccount ("ray"); return user;} @ RequestMapping (value = "/helloWorld2") public String helloWorld (User user) {user. setName ("Old Wang"); return "helloWorld. jsp ";}}
Name of the specified object for object merging:
@ Controllerpublic 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 ("Old Wang"); return "helloWorld. jsp ";}}
In this way, you can access
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.
Summary:
@ ModelAttribute has the following functions:
① Bind Request Parameters to a command object: when placed on the input parameters of the function processing method, it is used to bind multiple request parameters to a command object to simplify the binding process, the model data is automatically exposed for display on the view page. In fact, @ ModelAttribute is similar to model. addAttribute ("attributeName", abc) for view page display.
public String test(@ModelAttribute("user") UserModel user)
Here, an annotation @ ModelAttribute ("user") is added to the model object with the bound command object named "user" for display on the view page. You can use $ {user. username} on the view page to obtain the attributes of the bound command object.
② Exposure @ RequestMapping method return value is model data: when it is placed on the return value of the function processing method, the return value of the exposure function processing method is model data, which is used for display on the view page.
public @ModelAttribute("user2") UserModel test3(@ModelAttribute("user2") UserModel user)
You can see that the return value type is a command object type and is annotated by @ ModelAttribute ("user2"). At this time, the returned value is exposed to the model data (named user2) for the view to display.
The return value of @ ModelAttribute annotation will overwrite the command object with the same name of @ ModelAttribute annotation in the @ RequestMapping annotation method.
③ Expose the form reference object as model data: when the model data is placed on the general method (non-functional processing method) of the processor, it is the form reference object to be displayed for the form, such as the city to be selected during registration, and is automatically added to the model object before executing the function processing method (@ RequestMapping annotation method) for display on the view page;