SpringMVC之响应数据传出

Spring MVC除了在原生的request和session外还能怎么样把数据带给页面

1) 可以在方法处传入Map,或者Model或者ModelMap。给这些参数里面保存的所有数据都会放在所有的数据都会放在请求域中。可以在页面中获取

在PageContext、Request、Session、Application
Map(interface(jdk)) ModelMap(class)
Model(interface(spring))
2) 方法的返回值可以变为ModeAndView类型
既包含视图信息(页面地址)也包含模型数据(给页面带的数据)
而且数据是放在请求域中
request、session、application
3) Spring MVC提供了一种可以临时给Session域中保存数据的方式
使用一个注解 @SessionAttributes(只能标注在类上)
给BindingAwareModelMap中保存的数据,同时给session中放一份

Spring MVC输出模型数据概述

提供了以下几种途径输出模型数据:

  • ModelAndView: 处理方法返回值类型为 ModelAndView 时, 方法体即可通过该对象添加模型数据
  • Map 及 Model: 入参为 org.springframework.ui.Model、
    org.springframework.ui.ModelMap 或 java.uti.Map 时,处理方法返回时,Map 中的数据会自动添加到模型中。
  • @SessionAttributes: 将模型中的某个属性暂存到 HttpSession 中,以便多个请求之间可以共享这个属性
  • @ModelAttribute: 方法入参标注该注解后, 入参的对象就会放到数据模型中

处理模型数据之 ModelAndView

  1. 控制器处理方法的返回值如果为 ModelAndView, 则其既包含视图信息,也包含模型数据信息。
  2. 添加模型数据:
    1
    2
    MoelAndView addObject(String attributeName, Object attributeValue)
    ModelAndView addAllObject(Map<String, ?> modelMap)
  3. 设置视图:
    1
    2
    void setView(View view)
    void setViewName(String viewName)

实验代码

  1. 控制器方法
    1
    2
    3
    4
    5
    6
    @RequestMapping("testModelAndView")
    public ModelAndView testModelAndView() {
    ModelAndView modelAndView = new ModelAndView("success");
    modelAndView.addObject("time", new Date().toString());//放到requestScope中
    return modelAndView;
    }
  2. 页面链接
    1
    <a href="testModelAndView">testModelAndView</a>
  3. 成功页面,显示数据
    1
    time:${requestScope.time }

处理数据之Map

  • Spring MVC 在内部使用了一个 org.springframework.ui.Model 接口存储模型数据

    具体使用步骤

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@RequestMapping("testMap")
public String testMap(Map<String, Object>map) {
System.out.println("testMap->"+map.getClass());
map.put("map", "map");
return "testMap";
}
@RequestMapping("testModel")
public String testModel(Model model) {
System.out.println("testModel->"+model.getClass());
model.addAttribute("model", "model");
return "testmodel";
}
@RequestMapping("testModelMap")
public String testModelMap(ModelMap map) {
System.out.println("testModelMap->"+map.getClass());
map.put("modelmap", "modelmap");
return "testmodelMap";
}

界面显示相应的数据获取以及访问链接

1
2
3
4
5
6
http://localhost:8200/SpringMVC_Output/testModel
http://localhost:8200/SpringMVC_Output/testMap
http://localhost:8200/SpringMVC_Output/testModelMap
map: ${requestScode.map }
model: ${requestScode.model }
ModelMap: ${requestScope.modelmap }

处理模型数据之@SessionAttributes(推荐不用)

  • 若希望在多个请求之间共用某个模型属性数据,则可以在控制器类上标注一个 @SessionAttributes, Spring MVC 将在模型中对应的属性暂存到 HttpSession 中。
  • @SessionAttributes 除了可以通过属性名指定需要放到会话中的属性外,还可以通过模型属性的对象类型指定哪些模型属性需要放到会话中

    例如:

    1. @SessionAttributes(types=User.class) 会将隐含模型中所有类型为 User.class 的属性添加到会话中。
    2. @SessionAttributes(value={“user1”, “user2”})
    3. @SessionAttributes(types={User.class, Dept.class})
    4. @SessionAttributes(value={“user1”, “user2”}, types={Dept.class})

      SessionAttributes源码

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      package org.springframework.web.bind.annotation;
       
      import java.lang.annotation.Documented;
      import java.lang.annotation.ElementType;
      import java.lang.annotation.Inherited;
      import java.lang.annotation.Retention;
      import java.lang.annotation.RetentionPolicy;
      import java.lang.annotation.Target;
       
      @Target({ElementType.TYPE}) //说明这个注解只能应用在类型上面
      @Retention(RetentionPolicy.RUNTIME)
      @Inherited
      @Documented
      public @interface SessionAttributes { 
      String[] value() default {}; //推荐使用 
      Class<?>[] types() default {}; //范围太广 
      }

实验代码

  1. 类上添加注解
    1
    2
    3
    4
    5
    6
    7
    8
    9
    @Controller
    @SessionAttributes(value = {"modelmap"},types= {String.class})
    /**
    * @SessionAttributes
    *  除了可以通过属性名指定需要放到会话中的属性外(实际上是通过value指定key值),
    *  还可以通过模型属性的对象类型指定哪些模型属性需要放到会话中(实际上是通过types指定类型)
    * 注意:只能放在类的上面,不能修饰方法
    */
    public class OutputController {

注意:

1
2
3
④异常
//org.springframework.web.HttpSessionRequiredException: Session attribute 'user' required - not found in session
//出现这个异常,是@SessionAttributes(value={"user"},types={String.class})导致的,去掉类上的这个注解

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×