Map集合在Spring域中的存储形式

来源:互联网 发布:淘宝买家信誉评级良好 编辑:程序博客网 时间:2024/05/05 11:44

在SpringMVC中,常用到Map集合然后存入域中,然后发现在JSP中用EL取Map集合时取不到集合元素.

例如:

  1. 一般在JSP中用EL获取Map集合的写法为:${requestScope.map集合名.键名}
  2. 在SpringMVC在JSP中用EL获取Map集合的写法为 : {requestScope.键名}

实际上,SpringMVC在存储Map集合时,在某种情况下,会将Map集合的每一个键值对,进行request.setAttribute(K, V);所以在JSP的EL表达式中有时不用写Map集合的名字才能取到.

那么,在什么情况下Map集合的键值对才会进行request.setAttribute(K, V)呢?

下面进行测试:

    @RequestMapping("test1")    public ModelAndView test1(){        Map<String,User> map = new HashMap<String, User>();        map.put("admin",new User("admin","1234"));        //ModelAndView的第二个参数可以直接存Map集合        //但是它底层调用的实际是:mv.addAllObjects(map);        //所以在jsp中取值形式:${requestScope.admin.username }        //Map集合的键值对进行了request.setAttribute(K, V)        ModelAndView mv = new ModelAndView("index",map);        return mv;    }
    @RequestMapping("test2")    public ModelAndView test2(){        Map<String,User> map = new HashMap<String, User>();        map.put("admin",new User("admin","1234"));        ModelAndView mv = new ModelAndView("index");        //jsp中取值形式:${requestScope.admin.username }        mv.addAllObjects(map);        //jsp中取值形式:${requestScope.map.admin.username }        mv.addObject("map",map);        //addAllObjects和addObject两个方法同时存在时,两种形式都会存储        //在JSP中,两种EL都可以取到值,这两个方法不涉及优先级问题        return mv;    }
    @RequestMapping("test3")    public String test3(Map<String,Object> map){        //jsp中取值形式:${requestScope.admin.username }        map.put("admin",new User("admin","1234"));        return "index";    }
    //形式四:返回值为String,形参为Model    @RequestMapping("test4")    public String test4(Model model,HttpServletRequest request){        //添加一个List集合        List<User> list = new ArrayList<User>();        list.add(new User("list5","1234"));        model.addAttribute("list",list);        //添加一个Map集合        Map<String,User> map = new HashMap<String, User>();        map.put("user1",new User("map","1234"));        //注意:下面两个方法不同        //jsp中取值形式:${requestScope.user1.username }        model.addAllAttributes(map);        //jsp中取值形式:${requestScope.map.user1.username }        model.addAttribute("map",map);        //添加一个对象        model.addAttribute("user",new User("admin","1234"));        return "index";    }

结论:

  1. 若返回一个ModelAndView对象,通过构造ModelAndView(“url”,map)或者是ModelAndView的addAllObjects(map)存储Map集合的话,会将Map集合的所有键值对进行request.setAttribute(K, V),如果没有通过这两种方式存Map,而是掉用ModelAndView的addObject(“map”,map)则在Request域中存的是map集合,相当于request.setAttribute(“map”,map);
  2. 若方法形参为Map< String,Object>类型的对象,且在方法内部没有进行request.setAttribute(“map”,map),那么Map集合里的每一个键值对会进行request.setAttribute(K, V),存入域中
  3. 若返回一个Model对象,如果在Model中调用了addAllAttributes(map);存集合,那么会将Map集合的所有键值对进行request.setAttribute(K, V),如果调用的是addAttribute(“map”,map);则在Request域中存的是map集合,相当于request.setAttribute(“map”,map);

注意:关于Model对象的addAllAttributes的方法:
addAllAttributes(Collection);添加一个集合

这个方法是添加一个Collection集合,但是不会将Collection集合进行request.setAttribute(K, V),因为Collection没有键值,不会以键值对形式存在
所以,存储Collection集合,用addAttribute(“list”,list)方法

2 0