Spring in the Model, ModelMap and ModelAndView

 

1、Model(org.springframework.ui.Model)

Model is an interface, the method comprising addAttribute which implementing class is ExtendedModelMap.

ExtendedModelMap inherited ModelMap class, ModelMap class implements the Map interface.

public class ExtendedModelMap extends ModelMap implements Model

Model parameters passed to the page in the following ways:

 

@Controller
 public  class User1Controller { 
    
    Private  static  Final the Log Logger = LogFactory.getLog (User1Controller. Class ); 
    
    // @ModelAttribute modified method will first call at login, the reception method for receiving the incoming parameters jsp page 
    @ModelAttribute
     public  void UserModel (the LoginName String, String password, 
             Model Model) { 
        logger.info ( "UserModel" );
         // create a User object storage parameters passed jsp page 
        User2 the User = new new User2 (); 
        user.setLoginname (the LoginName); 
        the User. the setPassword (password); 
        // add to User Object Model which
        model.addAttribute ( "User" , User); 
    } 
    
    @RequestMapping (value = "/ Login1" )
      public String Login (Model Model) { 
        logger.info ( "Login" );
         // stored before removing them from the Model name the user object 
        User2 user = (User2) model.asMap () GET ( "user." ); 
        System.out.println (user); 
        // set the username of the user object properties 
        user.setUsername ( "test" );
         return "RESULT1" ; 
    }

Reception: (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>

result1.jsp

< HTML > 
< head > 
< Meta HTTP-equiv = "the Type-the Content" Content = "text / HTML; charset = UTF-. 8" > 
< title > method for testing @ModelAttribute (value = "") returns Note specific class < / title > 
</ head > 
< body > 
access request scope domain model object: $ {requestScope.user.loginname} < br > 
access request scope domain model object: $ requestScope.user.password} { < br > 
access request scope domain model object: $ {requestScope.user.username} < br >
<br>
</body>
</html>

operation result:

 

@ModelAttribute modified login process will first call in which the request parameter value assigned to the corresponding variables. Objects can be added to the methods Model, is provided to join a Model types of parameters in the process.

User1Controller.java can be simplified as:

@RequestMapping(value="/login1new")
    public String login(Model model, @ModelAttribute User2 user){
        user.setUsername("测试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 passed as a parameter controller method, users do not need to create their own objects. ModelMap main targets for transferring data to a method of treating jsp controller interface, the data in the process controller interface jsp need to put ModelMap object. Pass parameters to the page in the following ways:

 

 

 

3, ModelAndView (org.springframework.web.servlet.ModelAndView) ModelAndView object has two effects:

(1) Set the url address (which is the main difference between the ModelAndView and ModelMap).

(2) the method of processing data controller jsp pages transmitted, the data in the process controller interface jsp need to put ModelAndView object. Then return mv. SetAttribute method that functions as a request object. Pass parameters to the page in the following ways:

 

If the return value of the method is ModelAndView controller, i.e., it contains information data model, but also contains information view, this view contains SpringMVC using rendering model data, the model data may simply be regarded as a Map <String, Object> object.

@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);
        // 将User对象添加到ModelAndView的Model当中
        mv.addObject("user", user);
    }
    
    @RequestMapping(value= "/ login3" )
      public ModelAndView login3 (ModelAndView Music Videos) { 
        logger.info ( "login3" );
         // remove deposited before named user from among the objects Model ModelAndView 
        User2 user = (User2) mv.getModel ( ) .get ( "user" ); 
        System.out.println (user); 
        // set the username of the user object properties 
        user.setUsername ( "test" );
         // address of the branch, returns the set view name 
        mv.setViewName ( "result3" );
         return Music Videos; 
    }

 

Reception: result3.jsp

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

 operation result:

 

 

 

 

Details: https://www.cnblogs.com/zeroingToOne/p/8945066.html

Summary: In the process controller interface to the data required into jsp ModelAndView object (by addObject () method), the url provided parsed view jump (by setViewName () method). Data model, view a View (jump)

  @RequestMapping(value = "/get")
    public ModelAndView get(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
       modelAndView.setViewName("/cmfz/media/mediaManager");
       return modelAndView;
    }

 

Redirect:

  @RequestMapping(value = "/jumpLogin")
    public ModelAndView jumpLogin(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
      /*  modelAndView.addObject("id","");
        modelAndView.addObject("password","");*/
        modelAndView.setViewName("redirect:https://www.baidu.com/");
        return modelAndView;
    }

 

 

 

 

Guess you like

Origin www.cnblogs.com/lvhouhou/p/11984395.html