想要将数据拿到页面给展现出来,最容易想到的是放到WEB域中,然后再取出,这篇文章记录一下不同于传统的WEB域数据输出的几种方式

使用 Map、Model、ModelMap进行传值

Spring MVC 在内部使用了一个org.springframework.ui.Model接口存储模型数据 具体步骤:

  • 1、Spring MVC 在调用方法前会创建一个隐含的模型对象作为模型数据的存储容器。
  • 2、如果方法的入参为 Map 或 Model 类型,Spring MVC 会将隐含模型的引用传递给这些入参。在方法体内,开发者可以通过这个入参对象访问到模型中的所有数据,也可以向模型中添加新的属性数据。

示例:
Model演示

@RequestMapping("/outputData")
    public String output1(Model model){
        model.addAttribute("msg", "我是output1()");
        return "hello";
    }

jsp页面:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>${msg}</h1>
</body>
</html>

浏览器访问:

spring 打印所有controller接口及class 使用springmvc实现页面输出_数据

Map演示

@RequestMapping("outputData1")
    public String output2(Map map){
        map.put("msg", "我是output2()");
        return "hello";
    }

浏览器访问:

spring 打印所有controller接口及class 使用springmvc实现页面输出_html_02


ModelMap演示

@RequestMapping("outputData2")
    public String output3(ModelMap modelMap){
        modelMap.addAttribute("msg", "我是output3()");
        return "hello";
    }

浏览器访问:

spring 打印所有controller接口及class 使用springmvc实现页面输出_html_03


Map、Model、ModelMap:最终都是保存在 BindingAwareModelMap 在工作。这些参数里面保存的所有数据都会在请求域中,这几种方式传递的值只能通过request获取。

ModelAndView传值

控制器处理方法的返回值如果为 ModelAndView, 则其既包含视图信息,也包含模型数据信息。
添加模型数据:

  • MoelAndView addObject(String attributeName, Object attributeValue)
  • ModelAndView addAllObject(Map<String, ?> modelMap)
    设置视图:
  • void setView(View view)
  • void setViewName(String viewName)
  • 也可以在对象创建时通过构造器设置视图
@RequestMapping("/outputData3")
    public ModelAndView output4(){
        ModelAndView modelAndView = new ModelAndView("hello");
        modelAndView.addObject("msg", "我本楚狂人,凤歌笑孔丘");
        return modelAndView;
    }
    
      @RequestMapping("/outputData4")
    public ModelAndView output5(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("hello");
        modelAndView.addObject("msg", "我本楚狂人,凤歌笑孔丘");
        return modelAndView;
    }

访问任意一个结果都是一样的。

浏览器访问:

spring 打印所有controller接口及class 使用springmvc实现页面输出_@ModelAttribute_04

@SessionAttributes

  • 若希望在多个请求之间共用某个模型属性数据,则可以在控制器类上标注一个@SessionAttributes, Spring MVC 将在模型中对应的属性暂存到 HttpSession 中。
  • @SessionAttributes 除了可以通过属性名指定需要放到会话中的属性外,还可以通过模型属性的对象类型指定哪些模型属性需要放到会话中
  • @SessionAttributes(types=User.class) 会将隐含模型中所有类型为 User.class 的属性添加到会话中。
  • @SessionAttributes(value={“user1”, “user2”})
  • @SessionAttributes(types={User.class, Dept.class})
  • @SessionAttributes(value={“user1”, “user2”}, types={Dept.class})

示例:

@Controller
@SessionAttributes(value = "msg")
public class HelloController {

    @RequestMapping("/session1")
    public String sessionTest1(Model model){
        model.addAttribute("msg", "我本楚狂人,凤歌笑孔丘");
        return "hello";
    }

jsp页面:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>${requestScope.msg}</h1>
    <hr>
    <h1>${sessionScope.msg}</h1>
</body>
</html>

结构:

spring 打印所有controller接口及class 使用springmvc实现页面输出_html_05


注意:

  • 给BindingAwareModelMap中保存的数据,或者在ModelAndView中的数据,同时给session中放一份;
  • Value的值就是保存数据是要给session中放的数据的key,这个key的值要和在ModelAndView、BindingAwareModelMap中保存数据的key的值一样,否则不会保存到session中即:BindingAwareModelMap中保存了数据,但是value的值不包含保存数据的key,就不能存在session中去。
  • @SessionAttributes(value={“msg”, “haha”}):保存多个数据到session中去。
  • @SessionAttributes(types = {String.class}):根据保存在BindingAwareModelMap、ModelAndView中的数据的类型只要与这个注解设置的相同,就保存在session,数据的key与BindingAwareModelMap、ModelAndView中保存的数据的key相同。

@ModelAttribute

  • 在方法定义上使用 @ModelAttribute 注解:Spring MVC 在调用目标处理方法前,会先逐个调用在方法级上标注了 @ModelAttribute 的方法。
  • 在方法的入参前使用 @ModelAttribute 注解:可以从隐含对象中获取隐含的模型数据中获取对象,再将请求参数绑定到对象中,再传入入参将方法入参对象添加到模型中

使用场景
如果我们执行某一个请求映射方法之前,想要相对数据进行一些处理,则可以使用@ModelAttribute注解。

@RequestMapping("/peopleTest")
    public String test(@ModelAttribute("people") People people, Model model){
        System.out.println(people);
        return "hello";
    }
    
    
    @ModelAttribute
    public void before(Model model){
        People people = new People("张三", 18, new Department(1, "营销部"));
        model.addAttribute("people", people);
    }

浏览器访问:

spring 打印所有controller接口及class 使用springmvc实现页面输出_html_06


控制台输出:

spring 打印所有controller接口及class 使用springmvc实现页面输出_html_07

在before方法的参数Model中存放数据,实际上还是存放在BindingAwareModelMap中,标注在目标方法的参数中,通过key的值,取出刚刚在Map中保存的数据。

值的注意的是:在这两个方法中,参数ModelMap指的是同一个BindingAwareModelMap,@ModelAttribute标注的参数people,与存放在存放在Model中的是指向同一个.ModelAttribute执行流程图

spring 打印所有controller接口及class 使用springmvc实现页面输出_SpringMVC_08