The difference between Model, ModelMap and ModelAndView in Spring

1. Model (org.springframework.ui.Model)
Model is an interface that includes the addAttribute method, and its implementation class is ExtendedModelMap.
ExtendedModelMap inherits the ModelMap class, and the ModelMap class implements the Map interface.

public class ExtendedModelMap extends ModelMap implements Model

Model passes parameters to the page by:

Example: User1Controller.java

@Controller
public class User1Controller{
    
    private static final Log logger = LogFactory.getLog(User1Controller.class);
    
    // The method modified by @ModelAttribute will be called before login. This method is used to receive the parameters passed in by the front-end jsp page 
    @ModelAttribute
     public  void userModel(String loginname,String password,
             Model model) {
        logger.info( "userModel" );
         // Create a User object to store the parameters passed in by the jsp page 
        User2 user = new User2();
        user.setLoginname(loginname);
        user.setPassword(password);
        // Add the User object to the Model 
        model.addAttribute("user" , user);
    }
    
    @RequestMapping(value="/login1")
     public String login(Model model){
        logger.info( "login" );
         // Get the previously stored object named user from the Model 
        User2 user = (User2) model.asMap().get("user" );
        System.out.println(user);
        // Set the username property of the user object 
        user.setUsername("test" );
         return "result1" ; 
    }

Front desk:
(1) loginForm1.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试Model</title>
</head>
<body>
<h3>测试Model</h3>
<form action="login1new" method="post">
     <table>
         <tr>
             <td><label>登录名: </label></td>
             <td><input type="text" id="loginname" name="loginname" ></td>
         </tr>
         <tr>
             <td><label>密码: </label></td>
             <td><input type="password" id="password" name="password" ></td>
         </tr>
         <tr>
             <td><input id="submit" type="submit" value="登录"></td>
         </tr>
     </table>
</form>
</body>
</html>

(2) result1.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test @ModelAttribute(value="") annotation to return the method of concrete class</title>
</head>
<body> 
Access the model object in the request scope: ${requestScope.user.loginname } <br> 
Access the model object in the request scope: ${requestScope.user.password } <br> 
Access the request scope The model object in: ${requestScope.user.username } <br>
<br>
</body>
</html>

operation result:

  

The method modified by @ModelAttribute will be called before login, which assigns the request parameter value to the corresponding variable. You can add objects to the Model in the method, provided that a parameter of type Model is added to the method.

User1Controller.java can be simplified to:

@RequestMapping(value="/login1new")
    public String login(Model model, @ModelAttribute User2 user){
        user.setUsername( "Test 2" );
        System.out.println(user.toString());
        model.addAttribute("user", user);
        return "result1";
    }

2. ModelMap (org.springframework.ui.ModelMap)
Spring framework automatically creates an instance of modelmap and passes it in as a parameter of the controller method. Users do not need to create an object by themselves.
The ModelMap object is mainly used to transfer the data processed by the controller method to the jsp interface. In the controller method, the data required by the jsp interface can be placed in the ModelMap object. Its role is similar to the setAttribute method of the request object. Pass parameters to the page by:

Find the data in the ModelMap through the request at the view layer.
Modelmap itself cannot set the url of the page jump, you can set the url of the jump through the return value of the controller method

Example: User2Controller.java

@Controller
public class User2Controller{
    private static final Log logger = LogFactory.getLog(User2Controller.class);
    
    @ModelAttribute
    public void userMode2(String loginname,String password,
             ModelMap modelMap){
        logger.info( "userMode2" );
         // Create a User object to store the parameters passed in by the jsp page 
        User2 user = new User2();
        user.setLoginname(loginname);
        user.setPassword(password);
        // Add User object to ModelMap 
        modelMap.addAttribute("user" , user);
    }
    
    @RequestMapping(value="/login2")
     public String login2(ModelMap modelMap){
        logger.info( "login2" );
         // Get the previously stored object named user from ModelMap 
        User2 user = (User2) modelMap.get("user" );
        System.out.println(user);
        // Set the username property of the user object 
        user.setUsername("test" );
         return "result2" ;
    }

Foreground: result2.jsp

<body> 
Access the modelMap object in the request scope: ${requestScope.user.loginname } <br> 
Access the modelMap object in the request scope: ${requestScope.user.password } <br> 
Access the request scope The modelMap object in: ${requestScope.user.username } <br>
<br>
</body>

operation result:

  

3. ModelAndView (org.springframework.web.servlet.ModelAndView) The
ModelAndView object has two functions:
(1) Set the url address (this is also the main difference between ModelAndView and ModelMap).
(2) Transfer the data processed in the controller method to the jsp page, and put the data required by the jsp interface into the ModelAndView object in the controller method. Then return mv. Its role is similar to the setAttribute method of the request object. Pass parameters to the page through the following methods:

On the interface, you can obtain the data in ModelAndView through the el variable ${key}.

Example: User3Controller.java

@Controller
public class User3Controller{
    private static final Log logger = LogFactory.getLog(User3Controller.class);
    
    @ModelAttribute
    public void userMode3(String loginname,String password,
             ModelAndView mv){
        logger.info("userMode3");
        User2 user = new User2();
        user.setLoginname(loginname);
        user.setPassword(password);
        // Add the User object to the Model of ModelAndView 
        mv.addObject("user" , user);
    }
    
    @RequestMapping(value="/login3")
     public ModelAndView login3(ModelAndView mv){
        logger.info( "login3" );
         // Retrieve the previously stored object named user User2 from ModelAndView's Model 
        User2 user = (User2) mv.getModel().get("user" );
        System.out.println(user);
        // Set the username property of the user object 
        user.setUsername("test" );
         // Address jump, set the returned view name 
        mv.setViewName("result3");
        return mv;
    }

Foreground: result3.jsp

<body> 
Access data in ModelAndView: ${user.loginname} <br> 
Access data in ModelAndView: ${user.password} <br> 
Access data in ModelAndView: ${user.username} <br>
</body>

 

Guess you like

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