Spring 作用域传值
/** * * @param request 可以把原生servlet有的东西写在参数里,response,session等 * @param map 可以存在Map中 * @param model 可以存在model接口对象 中 * @return 跳转到一个jsp页面,因为配置了视图定位,所以只要号index */ @RequestMapping("demo13") public String demo13(HttpServletRequest request,Map<String,String> map,Model model) { request.setAttribute("request", "requestName"); request.getSession().setAttribute("session", "sessionName"); request.getServletContext().setAttribute("application","applicationName"); //SpringMVC会把map放在request中 map.put("map", "mapName"); model.addAttribute("model","modelName"); return "index"; }
在jsp中用el表达示取出:
<body> request:${requestScope.request }<br/> session:${sessionScope.session }<br/> application:${applicationScope.application }<br/> map:${requestScope.map }<br/> model:${requestScope.model }<br/> </body>
使用ModelAndView类:
@RequestMapping("demo14") public ModelAndView demo14() { //new ModeAndVie()的参数代表要进行跳转的路径 ModelAndView mode = new ModelAndView("index"); //存入一条数据。存在request中 mode.addObject("modeAndView","modeAndView"); return mode; }