理解springMVC中的Model和Session属性
作为一个javaweb应用的开发者,你快速学习了request(HttpRequest)和Session(HttpSession)的范围,理解这些范围并且在这些范围内数据和对象是如何是进出的对设计和构建web应用是非常关键的。
springMVC的范围
spring的@MODELATTRIBUTE
@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
}
在一个请求的request中,任何使用@ModelAttribute注解的方法会在controller的handler方法(像上边例子汇总的requestHandlingMethod 方法)之前被调用。在这些handler方法执行前,这些方法把数据增加到java.util.map中最终添加到spring Model中去。这可以通过一个简单的实验证明,我创建了两个jsp页面:index.jsp和nextpage.jsp。index.jsp中的链接用来发送request到web应用中来触发Mycontroller中的requestHandlingMethod()方法。requestHandlingMethod()方法返回“nextpage”作为下一个视图逻辑上的名字,在这个例子中我们解析为nextpage.jsp。
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 />
<%
}
%>
当应用打开并且index.jsp展现的时候,你可以看到在Request范围内没有属性
spring Model和Request后边的原因
Spring的@SESSIONATTRIBUTE
<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 />
<%
}
%>
我在MyController中用@SessionAttributes做注解,来把同样的model属性加到spring Session中去。
@Controller
@SessionAttributes("myRequestObject")
public class MyController {
...
}
我同样也在handler方法中增加了代码来展示什么属性在httpSession中
@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";
}
现在,当我们使用@SessionAttributes注解后我们可以看到什么在session对象中,包括springMVC处理一个http 请求的前中后的过程里。结果如下,首先是index.jsp展示,我们可以看到在HttpServletRequest和httpSession中都没有属性数据。
控制对话属性
美团招聘,有意向的私信我
岗位名称:美团到家事业群-外卖技术部-JAVA高级工程师
职级要求:P2-3~P3-2
岗位职责:
- 负责上单业务系统的开发工作,深入理解业务痛点,通过技术手段满足并促进业务快速发展需要;
- 参与系统需求分析与设计,快速响应业务需求,负责完成核心代码编写,接口规范制定;
- 参与解决项目中的重难点问题,针对复杂业务模型能够给出合理的解决方案;
岗位基本要求:
- 参与过大型复杂分布式互联网系统的设计,要求有复杂业务的开发经验和较强的逻辑/概率思维能力,善于分析、归纳、描述、沟通和解决问题;
- 本科及以上学历,扎实的计算机基础,4年及以上工作经验,长期使用Java及开源框架进行项目开发,并有一定的项目管理经验;
- 深入使用Java,熟悉掌握常用的Java类库及框架,如多线程、并发处理、I/O与网络通讯等,对Java虚拟机有较深了解;
- 擅长使用开源框架,在某个领域内对开源框架有一定的审美,做过选型研究,解读过部分或者全部源码,掌握实现原理;
- 精通 MySQL 应用开发,熟悉数据库原理和常用性能优化技术,以及 NoSQL,Queue 的原理、使用场景以及限制;
- 研究过 http 协议、搜索引擎、缓存、jvm 调优、序列化、nio、RPC 调用框架等,并且有相关实践经验;
- 有强烈的责任心和团队合作精神,良好的抗压能力,心态积极,能主动融入团队;
具备以下者优先:
- 参与过相关系统建设;
- 有高并发系统经验;
- 参与过开源工作;
- 有代码洁癖、有极客精神;