SpringMVC: Model data parsing

As an MVC framework, a very important job of the SpringMVC framework is to obtain model data from the controller and return it to the client, that is, to display the model data on the JSP page. The technology used is to obtain values ​​from domain objects through EL expressions.

 

In the Servlet, we can directly call the web resource to pass the value to the domain object. In the SpringMVC framework, how to complete this operation? Today we will learn the model data binding of the SpringMVC framework.

 

First of all, let's understand this sentence. Model data binding refers to binding model data to JSP domain objects. Review what are the domain objects?

 

The four built-in objects corresponding to the four major scopes of JSP are:

pageContext,request,session,application。

 

The binding of model data is done by ViewResolver. During development, we add model data first, and then hand it over to ViewResolver for binding.

 

So the focus of our learning is how to add model data.

 

SpringMVC provides the following ways to add model data:

Map

Model

ModelAndView

@SessionAttributes

@ModelAttribute

 

The domain objects often used in development are request and session, so for these two domain objects, pageContext and application can be bound by obtaining native Servlet resources, which are not used much in actual development.

 

Model data is bound to the request domain object

 

1.Map

SpringMVC uses the Model interface to store model data internally, and creates an implicit object as a storage container for model data before calling business methods.

Set the input parameter of the business method to the Map type, and SpringMVC will pass the reference of the implicit object to the input parameter. In the method body, developers can access all the data in the model through the input object, and can also add new data to the model.

That is, it is only necessary to add the input parameters of the Map type to the business method, and the model data can be added in the method body through the operation of the input parameters.

 

    @RequestMapping("/mapTest")
    public String mapTest(Map<String,Object> map){
        User user = new User();
        user.setId(1);
        user.setName("张三");
        map.put("user", user);
        return "index";
    }


After the business method is completed, the model data and view information are returned to the DispatcherServlet. The DispatcherServlet parses the view information through the ViewResolver, maps the logical view to the physical view, and binds the model data to the JSP request domain object. On the JSP page, you can directly pass the EL expression evaluates.

 

 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    ${user.name }
</body>
</html>

 

Start tomcat and run.

 

2.Model

Model is similar to Map, and the business method completes the binding of model data through input parameters.

 

    @RequestMapping("/modelTest")
    public String modelTest(Model model){
        User user = new User();
        user.setId(1);
        user.setName("张三");
        model.addAttribute("user", user);
        return "index";
    }

 

3.ModelAndView

Different from Map or Model, ModelAndView contains not only model data, but also view information. Therefore, when ModelAndView is used to process model data, the return value of the business method must be ModelAndView.

Two operations are performed on ModelAndView in the business method: 1. Fill model data. 2. Binding view information.

 

There are 8 ways to use ModelAndView.

 

    @RequestMapping("/modelAndViewTest1")
    public ModelAndView modelAndViewTest1(){
        ModelAndView modelAndView = new ModelAndView();
        User user = new User();
        user.setId(1);
        user.setName("张三");
        modelAndView.addObject("user", user);
        modelAndView.setViewName("index");
        return modelAndView;
    }

    @RequestMapping("/modelAndViewTest2")
    public ModelAndView modelAndViewTest2(){
        ModelAndView modelAndView = new ModelAndView();
        User user = new User();
        user.setId(1);
        user.setName("张三");
        modelAndView.addObject("user", user);
        View view = new InternalResourceView("/index.jsp");
        modelAndView.setView(view);
        return modelAndView;
    }

    @RequestMapping("/modelAndViewTest3")
    public ModelAndView modelAndViewTest3(){
        ModelAndView modelAndView = new ModelAndView("index");
        User user = new User();
        user.setId(1);
        user.setName("张三");
        modelAndView.addObject("user", user);
        return modelAndView;
    }

    @RequestMapping("/modelAndViewTest4")
    public ModelAndView modelAndViewTest4(){
        View view = new InternalResourceView("/index.jsp");
        ModelAndView modelAndView = new ModelAndView(view);
        User user = new User();
        user.setId(1);
        user.setName("张三");
        modelAndView.addObject("user", user);
        return modelAndView;
    }

    @RequestMapping("/modelAndViewTest5")
    public ModelAndView modelAndViewTest5(){
        Map<String,Object> map = new HashMap<String,Object>();
        User user = new User();
        user.setId(1);
        user.setName("张三");
        map.put("user", user);
        ModelAndView modelAndView = new ModelAndView("index", map);
        return modelAndView;
    }

    @RequestMapping("/modelAndViewTest6")
    public ModelAndView modelAndViewTest6(){
        Map<String,Object> map = new HashMap<String,Object>();
        User user = new User();
        user.setId(1);
        user.setName("张三");
        map.put("user", user);
        View view = new InternalResourceView("/index.jsp");
        ModelAndView modelAndView = new ModelAndView(view, map);
        return modelAndView;
    }

    @RequestMapping("/modelAndViewTest7")
    public ModelAndView modelAndViewTest7(){
        User user = new User();
        user.setId(1);
        user.setName("张三");
        ModelAndView modelAndView = new ModelAndView("index", "user", user);
        return modelAndView;
    }

    @RequestMapping("/modelAndViewTest8")
    public ModelAndView modelAndViewTest8(){
        User user = new User();
        user.setId(1);
        user.setName("张三");
        View view = new InternalResourceView("/index.jsp");
        ModelAndView modelAndView = new ModelAndView(view, "user", user);
        return modelAndView;
    }

 

4.HttpServletRequest

SpringMVC can directly obtain the Servlet native web resources in the business method, just add the HttpServletRequest input parameter when the method is defined, and in the method body, you can directly operate the request object.

 

    @RequestMapping("requestTest")
    public String requestTest(HttpServletRequest request){
        User user = new User();
        user.setId(1);
        user.setName("张三");
        request.setAttribute("user", user);
        return "index";
    }

 

5. @ ModelAttribute

SpringMVC can also add model data through @ModelAttribute annotation. How to use it?

1. Define a method that returns the object to be populated into the model data.

2. Add the @ModelAttribute annotation to the method.

Note that this method is not a business method that responds to the request.

 

    @RequestMapping("/modelAttributeTest")
    public String modelAttributeTest(){
        return "index";
    }

    @ModelAttribute
    public User getUser(){
        User user = new User();
        user.setId(1);
        user.setName("张三");
        return user;
    }

 

The method annotated with @ModelAttribute will be called automatically before SpringMVC calls any business method.

 

Therefore, before executing the modelAttributeTest business method, the getUser method will be called first to obtain the return value user object, and the SpringMVC framework will automatically fill the object into the model data, and then bind it to the domain object.

 

We know that the data in the domain object is stored in the form of key-value pairs, so what is the key at this time? By default, the first letter of the class name of the class corresponding to the model data is lowercase, that is, the first letter of the User class is lowercase "user", so in the JSP page, you can directly use "user" to get the value.

 

If getUser does not put back the value, you must manually fill in the model data in this method, using Map or Model.

 

    @ModelAttribute
    public void getUser2(Map<String,Object> map){
        User user = new User();
        user.setId(1);
        user.setName("张三");
        map.put("user", user);
    }

    @ModelAttribute
    public void getUser3(Model model){
        User user = new User();
        user.setId(1);
        user.setName("张三");
        model.addAttribute("user", user);
    }

 

Model data is bound to the session domain object

 

All the above methods are to bind the model data to the request object. If you need to bind the model data to the session object, you only need to add the @SessionAttributes(value="user") annotation to the class definition.

 

@Controller
@SessionAttributes(value="user")
public class HelloHandler {

 

At this point, no matter which of the above methods is used to execute the business code, when the model data is bound to the request object, the model data is also bound to the session object, and the model data exists in both the request and session objects.

 

In addition to binding by key value, @SessionAttributes can also be bound by the data type of model data.

 

@Controller
@SessionAttributes(types=User.class)
public class HelloHandler {

 

@SessionAttributes can bind multiple model data at the same time.

 

@Controller
@SessionAttributes(value={"user","address"})
public class HelloHandler {

 

@Controller
@SessionAttributes(types={User.class,Address.class})
public class HelloHandler {

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325622380&siteId=291194637