SpringMVC域对象数据输出

在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