作为一个javaweb应用的开发者,你快速学习了request(HttpRequest)和Session(HttpSession)的范围,理解这些范围并且在这些范围内数据和对象是如何是进出的对设计和构建web应用是非常关键的。
@Controller public class MyController { @ModelAttribute("myRequestObject") public MyCommandBean addStuffToRequestScope() { System.out.println("Inside of addStuffToRequestScope"); MyCommandBean bean = new MyCommandBean("Hello World",42); return bean; } @RequestMapping("/dosomething") public String requestHandlingMethod(Model model, HttpServletRequest request) { System.out.println("Inside of dosomething handler method"); System.out.println("--- Model data ---"); Map modelMap = model.asMap(); for (Object modelKey : modelMap.keySet()) { Object modelValue = modelMap.get(modelKey); System.out.println(modelKey + " -- " + modelValue); } System.out.println("=== Request data ==="); java.util.Enumeration reqEnum = request.getAttributeNames(); while (reqEnum.hasMoreElements()) { String s = reqEnum.nextElement(); System.out.println(s); System.out.println("==" + request.getAttribute(s)); } return "nextpage"; } // ... the rest of the controller }
Inside of addStuffToRequestScope Inside of dosomething handler method --- Model data --- myRequestObject -- MyCommandBean [someString=Hello World, someNumber=42] === Request data === org.springframework.web.servlet.DispatcherServlet.THEME_SOURCE ==WebApplicationContext for namespace 'dispatcher-servlet': startup date [Sun Oct 13 21:40:56 CDT 2013]; root of context hierarchy org.springframework.web.servlet.DispatcherServlet.THEME_RESOLVER ==org.springframework.web.servlet.theme.FixedThemeResolver@204af48c org.springframework.web.servlet.DispatcherServlet.CONTEXT ==WebApplicationContext for namespace 'dispatcher-servlet': startup date [Sun Oct 13 21:40:56 CDT 2013]; root of context hierarchy org.springframework.web.servlet.HandlerMapping.pathWithinHandlerMapping ==dosomething.request org.springframework.web.servlet.HandlerMapping.bestMatchingPattern ==/dosomething.* org.springframework.web.servlet.DispatcherServlet.LOCALE_RESOLVER ==org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver@18fd23e4现在的问题是“springModel数据存储在哪?”它存储在标准的java request范围中吗?答案是“是的”,从上边的输出可以看出,当handler方法执行的时候MyCommandBean是在model中,但是没有在request对象中。确实,spring不会把model数据作为request的属性,直到执行handler方法之后和下一个视图之前(在这个例子中是nextpage.jsp)
<hr /> <h3>Request Scope (key==values)</h3> <% java.util.Enumeration<String> reqEnum = request.getAttributeNames(); while (reqEnum.hasMoreElements()) { String s = reqEnum.nextElement(); out.print(s); out.println("==" + request.getAttribute(s)); %><br /> <% } %>
<h3>Session Scope (key==values)</h3> <% java.util.Enumeration<String> sessEnum = request.getSession() .getAttributeNames(); while (sessEnum.hasMoreElements()) { String s = sessEnum.nextElement(); out.print(s); out.println("==" + request.getSession().getAttribute(s)); %><br /> <% } %>
@Controller @SessionAttributes("myRequestObject") public class MyController { ... }
@SuppressWarnings("rawtypes") @RequestMapping("/dosomething") public String requestHandlingMethod(Model model, HttpServletRequest request, HttpSession session) { System.out.println("Inside of dosomething handler method"); System.out.println("--- Model data ---"); Map modelMap = model.asMap(); for (Object modelKey : modelMap.keySet()) { Object modelValue = modelMap.get(modelKey); System.out.println(modelKey + " -- " + modelValue); } System.out.println("=== Request data ==="); java.util.Enumeration<String> reqEnum = request.getAttributeNames(); while (reqEnum.hasMoreElements()) { String s = reqEnum.nextElement(); System.out.println(s); System.out.println("==" + request.getAttribute(s)); } System.out.println("*** Session data ***"); Enumeration<String> e = session.getAttributeNames(); while (e.hasMoreElements()){ String s = e.nextElement(); System.out.println(s); System.out.println("**" + session.getAttribute(s)); } return "nextpage"; }