标签:maven controller spring mvc sessionattributes modelattribute
这篇文章我们继续来研究springMVC的controller。
在springMvc中提供了两个标签,@ModelAttribute和@SessionAttributes,有了这两个标签可以让代码模块化更加方便。先上代码:
@Controller @RequestMapping("testAttr") @SessionAttributes (value={ "user","time"}, types={User. class }) public class AttributeController { @ModelAttribute("time") public String getTime() { SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date=new Date(); String result = sdf.format(date); System.out.println("getTime-------->"+result); return result; } @ModelAttribute("timeLong") public long getTimeLong() { SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date=new Date(); System.out.println("getTimeLong-------->"+date.getTime()); return date.getTime(); } @RequestMapping("testUser") public void getUser(Map<String, Object> map, Writer writer) throws IOException { Date date=new Date(); User user=new User(); user.setName("name"+date.getTime()); System.out.println("getUser-------->"+user.getName()); map.put("user", user); writer.write("\nUser is : " + user.getName() ); } @RequestMapping("testAttribute") public void testAttribute(@ModelAttribute("time") String time,@ModelAttribute("timeLong") long timeLong, @ModelAttribute("user") User user,Writer writer, HttpSession session) { try { writer.write("\nTime is : " + time ); writer.write("\nTimeLong is : " + timeLong ); writer.write("\nUser is : " + user.getName() ); User userSession=(User) session.getAttribute("user"); writer.write("\nsessionTime:"+session.getAttribute("time")); writer.write("\nsessionTimeLong:"+session.getAttribute("timeLong")); writer.write("\nsessionUser:"+userSession); if(userSession!=null){ writer.write("\nsessionUserName:"+userSession.getName()); } } catch (IOException e) { e.printStackTrace(); } } }
看一下效果吧。
首先,直接请求http://127.0.0.1:8080/test_web/testAttr/testAttribute,会发现报错了。错误内容如下:
org.springframework.web.HttpSessionRequiredException: Expected session attribute 'user' org.springframework.web.method.annotation.ModelFactory.initModel(ModelFactory.java:103) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:726) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689) org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852) javax.servlet.http.HttpServlet.service(HttpServlet.java:624) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837) javax.servlet.http.HttpServlet.service(HttpServlet.java:731) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)为什么呢?
我们看到代码中@SessionAttributes指定了在session中用到的两个参数:"user"、"time",但是为什么会报错呢?如果把testAttribute方法的user参数去掉就不会报错,这是因为,getTime()方法被@ModelAttribute("time")标记为模型,并且testAttribute的参数是@ModelAttribute标记的,这样的话testAttribute首先会去调用getTime()方法,并且把返回值time传入到testAttribute中,getTimeLong()同理。另外time时被SessionAttributes标记的,所以它的值会被存到session中,而“timeLong”则不会。但是加上@ModelAttribute("user") String user之后就会报错,是因为user还没有被添加。
然后,我们先请求http://127.0.0.1:8080/test_web/testAttr/testUser,然后再请求http://127.0.0.1:8080/test_web/testAttr/testAttribute,这样就不会报错,效果如下图所示:
我们发现testAttribute方法的所欲参数都是有值的,session中没有找到timeLong参数。另外需要补充的是像Writer writer这样会自动赋值的数据类型还有HttpServletRequest、HttpServletResponse、HttpSession、InputStream、OutputStream、Reader、Writer、Model、ModelMap、MultipartFile、Errors、BindingResult 等。
到此我们需要注意的是,如果我们刷新testAttribute,页面上除了TimeLong is那一行会刷新之外,其他数据是没有改变的,因为其他数据都是优先从session中获取的。如果我们再次请求testUser,然后在刷新testAttribute,会发现User相关的数据是改变了,如下图:
我们看一下后台打印,会更加清楚:
getTime-------->2015-08-28 19:16:05 getTimeLong-------->1440760565867 getUser-------->name1440760565875 getTimeLong-------->1440760570465 getTimeLong-------->1440760638091 getTimeLong-------->1440760639985 getTimeLong-------->1440760640793 getTimeLong-------->1440760641320 getTimeLong-------->1440760641872 getTimeLong-------->1440760642536 getTimeLong-------->1440760648097 getTimeLong-------->1440760648592 getTimeLong-------->1440760649072 getTimeLong-------->1440760649512 getTimeLong-------->1440760650024 getTimeLong-------->1440760650376 getTimeLong-------->1440760650720 getTimeLong-------->1440760650952 getTimeLong-------->1440760651167 getTimeLong-------->1440760651391 getTimeLong-------->1440760651656 getTimeLong-------->1440760711522 getUser-------->name1440760711523 getTimeLong-------->1440760716985 getTimeLong-------->1440760745147 getTimeLong-------->1440760937057
版权声明:本文为博主原创文章,未经博主允许不得转载。
maven与springMVC之ModelAttribute、SessionAttributes
标签:maven controller spring mvc sessionattributes modelattribute
原文地址:http://blog.csdn.net/w2865673691/article/details/48056161