首页 > 其他 > 详细

域对象-数据输出

时间:2019-06-23 18:43:59      阅读:102      评论:0      收藏:0      [点我收藏+]

在request、session、application雨中保存数据,进行数据的传递:

SpringMVC除了可以在方法上传入request和session外还能怎么样吧数据带给页面:

  1、方法返回值是String,可以在方法出传入Map、Model、ModelMap。这些参数里面保存的所有数据都会放在request域中,可以在页面获取;

    关系:Map、Model、ModelMap,最终都是BindingAwareModelMap在工作;相当于在BindingAwareModelMap中保存的东西都会被放在请求域中;

    Map(interface[jdk])  Model(interface[Spring])  

    技术分享图片

@RequestMapping("input")
public String input(Map map, ModelMap modelMap, Model model){
    map.put("msg","hello") ;
    modelMap.addAttribute("name","word") ;
    model.addAttribute("age",17) ;
    return "message/index" ;
}

 

  2、方法的返回值可以变为ModelAAndView属性:

    即包含视图信息(页面地址)也包含模型数据(给页面带的数据),而且数据时放在请求域中;

@RequestMapping("input")
public ModelAndView input(HttpServletRequest request , HttpServletResponse response){
    ModelAndView modelAndView = new ModelAndView("message/index");
    modelAndView.addObject("msg","hello") ;
    return modelAndView ;
}

  3、给session域中保存数据:

@RequestMapping("input")
public ModelAndView input(){
    HttpSession session = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest().getSession() ;
    session.setAttribute("msg","hello");
    return new ModelAndView("message/index") ;
}

 

 

域对象-数据输出

原文:https://www.cnblogs.com/luliang888/p/11073623.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!