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

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

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

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

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

    SpringMVC域对象-数据输出

@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") ;
}

 

 

相关文章:

  • 2021-04-19
  • 2022-02-01
  • 2022-12-23
  • 2021-09-06
  • 2021-11-26
  • 2022-03-08
  • 2022-12-23
  • 2021-11-04
猜你喜欢
  • 2021-08-29
  • 2022-12-23
  • 2021-12-26
  • 2022-02-03
  • 2022-12-23
  • 2021-10-15
  • 2022-12-23
相关资源
相似解决方案