当Controller组件处理后,向jsp页面传值,
1:使用HttpServletRequest 和 Session 然后setAttribute(),就和Servlet中一样
2:使用ModelAndView对象
3:使用ModelMap对象
4:使用@ModelAttribute注解
ModelAndView:
ModelAndView有两个作用,通过ModelAndView构造方法可以指定返回的页面名称,也可以通过setViewName()方法跳转到指定的页面。用于传递控制方法处理结果数据到结果页面,也就是说我们把需要在结果页面上需要的数据放到ModelAndView对象中即可,他的作用类似于request对象的setAttribute方法的作用,用来在一个请求过程中传递处理的数据。通过以下方法向页面传递参数: addObject(String key,Object value);
@RequestMapping("/test")
public ModelAndView show1(HttpServletRequest request, HttpServletResponse response) throwsException {
ModelAndView modelAndView = new ModelAndView("/test");
modelAndView.addObject("type", "type");
return modelAndView;
}
ModelMap :