zoukankan      html  css  js  c++  java
  • Spring中Model,ModelMap和ModelAndView

     目录

    1、Model接口

    2、ModelMap

    3、ModelAndView

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

    Model是一个接口,包含addAttribute方法,其实现类是ExtendedModelMap。

    ExtendedModelMap继承了ModelMap类,ModelMap类实现了Map接口。

    public class ExtendedModelMap extends ModelMap implements Model

    Model通过以下方法向页面传递参数:

    @Controller
    public class User1Controller{
        
        private static final Log logger = LogFactory.getLog(User1Controller.class);
        
        // @ModelAttribute修饰的方法会先于login调用,该方法用于接收前台jsp页面传入的参数
        @ModelAttribute
        public void userModel(String loginname,String password,
                 Model model){
            logger.info("userModel");
            // 创建User对象存储jsp页面传入的参数
            User2 user = new User2();
            user.setLoginname(loginname);
            user.setPassword(password);
            // 将User对象添加到Model当中
            model.addAttribute("user", user);
        }
        
        @RequestMapping(value="/login1")
         public String login(Model model){
            logger.info("login");
            // 从Model当中取出之前存入的名为user的对象
            User2 user = (User2) model.asMap().get("user");
            System.out.println(user);
            // 设置user对象的username属性
            user.setUsername("测试");
            return "result1";
        }

    前台: (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="Content-Type" content="text/html; charset=UTF-8">
    <title>测试@ModelAttribute(value="")注释返回具体类的方法</title>
    </head>
    <body>
    访问request作用范围域中的model对象:${requestScope.user.loginname }<br>
    访问request作用范围域中的model对象:${requestScope.user.password }<br>
    访问request作用范围域中的model对象:${requestScope.user.username }<br>
    <br>
    </body>
    </html>

    运行结果:

    @ModelAttribute修饰的方法会先于login调用,它把请求参数值赋给对应变量。可以向方法中的Model添加对象,前提是要在方法中加入一个Model类型的参数。

    User1Controller.java可以简化为:

    @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框架自动创建modelmap的实例,并作为controller方法的参数传入,用户无需自己创建对象。 ModelMap对象主要用于把controller方法处理的数据传递到jsp界面,在controller方法中把jsp界面需要的数据放到ModelMap对象中即可。 通过以下方法向页面传递参数:

    3、ModelAndView(org.springframework.web.servlet.ModelAndView)

    ModelAndView对象有两个作用:

    (1) 设置url地址(这也是ModelAndView和ModelMap的主要区别)。

    (2) 把controller方法中处理的数据传到jsp页面,在controller方法中把jsp界面需要的数据放到ModelAndView对象中即可。然后return mv。它的作用类似request对象的setAttribute方法。通过以下方法向页面传递参数:

    controller方法的返回值如果是ModelAndView,则其即包含模型数据信息,又包含视图信息,这样SpringMVC将使用包含的视图对模型数据进行渲染,可以简单地将模型数据看成一个Map<String, 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 mv){
            logger.info("login3");
            // 从ModelAndView的Model当中取出之前存入的名为user的对象
            User2 user = (User2) mv.getModel().get("user");
            System.out.println(user);
            // 设置user对象的username属性
            user.setUsername("测试");
            // 地址跳转,设置返回的视图名称
            mv.setViewName("result3");
            return mv;
        }

    前台:result3.jsp

    <body>
    访问ModelAndView中的数据:${user.loginname}<br>
    访问ModelAndView中的数据:${user.password}<br>
    访问ModelAndView中的数据:${user.username}<br>
    </body>

     运行结果:

     

    详情:https://www.cnblogs.com/zeroingToOne/p/8945066.html

    总结:在controller方法中把jsp界面需要的数据放到ModelAndView对象中(通过addObject()方法) ,设置解析视图后跳转的url地址(通过setViewName()方法) 。数据的model,视图是View(跳转)

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

    重定向:

      @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;
        }
  • 相关阅读:
    LRT最大似然比检验
    EPNP理论分析
    奇异值SVD分解
    矩阵求导
    static_cast和dynamic_cast用法
    Django 使用 Celery 实现异步任务
    python爬虫实战一:分析豆瓣中最新电影的影评
    scrapy模拟登陆知乎--抓取热点话题
    一个小时搭建一个全栈Web应用框架(上)
    一个小时搭建一个全栈 Web 应用框架(下)——美化与功能
  • 原文地址:https://www.cnblogs.com/lvhouhou/p/11984395.html
Copyright © 2011-2022 走看看