一.SpringMVC的四种传值方式
1.传统Servlet进行传值
@ResquestMapping("demo1")
public String demo1(HttpServletRequest req,HttpSession session){
req.setAttribute("req","req的值");
HttpSession session=req.getSession();
session.setAttribute("session","session的值");
sessionParam.setAttribute("sessionParam","sessionParam的值");
ServletContext application=new ServletContext();
application.setAttribute("application","application的值");
return "/index.jsp";
}
2.通过Map传值(底层由request接口实现)
@RequestMapping("demo2")
public String demo2(Map<String,Object>map){
map.put("map","map的值");
return "/index.jsp";
}
3.使用SpringMVC中的Model接口传值
@RequestMapping("demo3")
public String demo3(Model model){
model.setAttribute("model","model的值");
return "/index.jsp";
}
4.使用SpringMVC中的ModelAndView接口传值(相当于Model+视图)
@RequestMapping("demo4")
public ModelAndView demo4(){
//参数,跳转视图
ModelAndView nav=new ModelAndView("/index.jsp");
nav.addObject("nav","nav的值");
return nav;
}
5.jsp代码
<body>
request:${requestScope.req}
session:${seesionScope.session}
sessionParam:${sessionScope.sessionParam}
application:${applicationScope.application}
map:${requestScope.map}
model:${model}
</body>