zoukankan      html  css  js  c++  java
  • SpringMVC中Model机制

    使用model向request作用域中存数据

    ps:当model中存入了数据,如果使用重定向跳转方式,那么SpringMVC会自动将model中的数据存放在地址栏中传递

    • 向session作用域中存入数据,需要在类上加一个注解:

      @SessionAttributes({"属性名..."})

      通过这个注解,可以指定将model中的那些命名属性存入session作用域中一份

    @Controller
    @RequestMapping("/model")
    public class ModelController {
    
        @RequestMapping("/modelTest1")
        public String modelTest1(Model model, HttpServletRequest request){
            //向request作用域存入数据
            request.setAttribute("rname","蓝银皇!");
            //使用model向request作用域中存数据
            model.addAttribute("mname","昊天锤!");
            return "index";
        }
    

      

    使用Model,map存入数据

    @RequestMapping("/modelMap")
        public String modelMap(Model model){
            //向request作用域存数据
            Map<String, String> map = new HashMap<>();
            map.put("dadada","大大大怪兽");
            map.put("shudaixiong","树袋熊");
            model.addAllAttributes(map);
    
            return "success";
    
        }
    

      

    使用ModelAndView进行数据和视图的处理

        /**
         * 使用modelAndView进行数据和视图的处理
         * @return
         */
        @RequestMapping("/mv")
        public ModelAndView modelAndView(){
            ModelAndView mv = new ModelAndView();
            //可以向作用域存入数据
            mv.addObject("name","大大大怪兽");
            //可以设置跳转的视图:参数书写的方式与方法的返回值为String书写的方式一样
            mv.setViewName("forward:/success.jsp");
            return mv;
        }
  • 相关阅读:
    排序
    wine-qq 安装
    逆元模板
    最长上升子序列
    SGU[115] Calendar
    SGU[123] The sum
    SGU[105] Div 3
    SGU[102] Coprimes
    SGU[100] A+B
    poj-1325-Machine Schedule
  • 原文地址:https://www.cnblogs.com/ljy9/p/14016562.html
Copyright © 2011-2022 走看看