当前位置:   article > 正文

SpringMVC输出数据_springmvc流式输出数据

springmvc流式输出数据
package com.atguigu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * springMVC除过在方法上传入原生的request与session外还能怎么样把数据带给页面?
 *
 * 1、在方法出传入Map或者Model或ModelMap。
 * 这参数里面保存的所有数据都会放在请求域中,可以在页面获取
 *  关系:Map、Model、ModelMap最终都是BindingAwareModelMap在工作,
 *  相当于BindingAwareModelMap中保存的东西都会放在请求域中。
 *
 *  Map(interface(JDK))      Model(interface(Spring))
 *       || (implement)                 //
 *       ||                            //
 *       \/                           //
 *  ModelMap(class)                  //
 *        (extends) \\              //(implement)
 *                  \/             \/
                     *  ExtendedModelMap
                     *          ||
                     *          ||(extends)
                     *          \/
                     *  BindingAwareModelMap
 *
 *  2、方法的返回值可以变为ModelAndView类型;
 *  既包含视图信息(页面地址),也包含模型数据(给页面带的数据),而且数据是放在request域中的。
 *
 *  3、Springmvc提供了一种可以临时给session域中保存数据的方式
 *  使用一个注解@SessionAttribute(只能标在类上)
 *  @SessionAttributes(value = "msg"):给BindingAwareModelMap或者ModelAndView中保存的msg数据同时给session中放一份
 *      value:指定保存数据时要给session中放的数据的key
 */

/*
    value = {"msg","msg1"}表示key是msg或msg1的数据同时给session中放一份
    value = {"msg","msg1"},types = {String.class}表示key是msg或msg1或类型是String的数据同时给session中放一份

    不建议使用SessionAttributes,可能会引发异常
    给session中存放数据推荐原生API
 */
@SessionAttributes(value = {"msg","msg1"},types = {String.class})
@Controller
public class OutoutController {

    //用Map带数据到request域中
    @RequestMapping("/handle01")
    public String handle01(Map<String,Object> map) {
        System.out.println("handle01执行了,map类型为:"+map.getClass());
        //handle01执行了,map类型为:class org.springframework.validation.support.BindingAwareModelMap
        map.put("msg","你好");
        return "success";
    }

    /**
     * @param model 一个接口 ,用Model带数据到request域中
     */
    @RequestMapping("/handle02")
    public String handle02(Model model){
        System.out.println("handle02执行了,model类型为:"+model.getClass());
        //handle02执行了,model类型为:class org.springframework.validation.support.BindingAwareModelMap
        model.addAttribute("msg","你好");
        return "success";
    }

    //用ModelMap带数据到request域中
    @RequestMapping("/handle03")
    public String handle03(ModelMap modelMap){
        System.out.println("handle03执行了,modelMap的类型为:"+modelMap.getClass());
        modelMap.addAttribute("msg","你好");
        //handle03执行了,modelMap的类型为:class org.springframework.validation.support.BindingAwareModelMap
        return "success";
    }

    //通过返回值为ModelAndView带数据到request域中
    @RequestMapping("/handle04")
    public ModelAndView handle04(){
        //之前的返回值就叫视图名,视图解析器是会帮我们最终拼串得到页面的真实地址
        ModelAndView mv = new ModelAndView("success");
        mv.addObject("msg","你好handle04");
        return mv;
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/568502
推荐阅读
相关标签
  

闽ICP备14008679号