Spring MVC详解
1.
package com.gary.util.spring;
package com.gary.util.spring;
2.
3. import javax.servlet.http.HttpServletRequest;
4. import javax.servlet.http.HttpServletResponse;
5.
6. import org.apachemons.logging.Log;
7. import org.apachemons.logging.LogFactory;
8. import org.springframework.beans.factory.InitializingBean;
9. import org.springframework.web.servlet.ModelAndView;
10. import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
11.
12. /**
13. * Spring MVC
14. * @author gary
15. *
16. */
17. public class CommonInterceptor extends HandlerInterceptorAdapter implements InitializingBean{
18.
19. static Log log =&Log(CommonInterceptor.class);
20.
21. /**
22. * 在系统启动时执行
23. */
24. public void afterPropertiesSet() throws Exception {
25. log.debug("=======初始化CommonInterceptor=========");
26. }
27.
28. /**
29. * 在Controller方法前进行拦截
30. * 如果返回false
31. * 从当前往回执行所有的afterCompletion方法,再退出链.
32. * 如果返回true
33. * 执行下一个,直到所有都执行完毕.
34. * 再运行被拦截的Controller.
35. * 然后进入链,从最后一个往回运行所有的postHandle方法.
36. * 接着依旧是从最后一个往回执行所有的afterCompletion方法.
37. */
38. @Override
39. public boolean preHandle(HttpServletRequest request,
40. HttpServletResponse response, Object handler) throws Exception {
41. log.debug("=====preHandle====");
42. //业务逻辑
43.
44. return true;
45. }
46.
47. @Override
48. public void postHandle(HttpServletRequest request,
49. HttpServletResponse response, Object handler,
50. ModelAndView modelAndView) throws Exception {
51. log.debug("==========postHandle=========");
52.
53. if(modelAndView != null){
54. String viewName =&ViewName();
55. log.debug("view name : " + viewName);
56. }else{
57. log.debug("view is null");
58. }
59. }
60.
61. /**
62. * 在Controller方法后进行拦截
63. * 当有抛出异常时,会从当前往回执行所有的afterCompletion方法
64. */
65. @Override
66. public void afterCompletion(HttpServletRequest httpservletrequest,
67. HttpServletResponse httpservletresponse, Object obj,
68. Exception exception) throws Exception {
69. log.debug("=====afterCompletion====");
70. }
71.
72.
73. }
部分配置
Xml代码
1. <property name="interceptors">
2. <list>
3. <bean class="com.gary.util.spring.CommonInterceptor"/>
4. <bean class="com.gary.util.spring.CommonInterceptor2"/>
5. </list>
6. </property>
实际运行结果(控制台DEBUG输出)
Log代码
1. DispatcherServlet with name 'springmvc' processing GET request for [/demo/Demo/test.html]
2. Matching patterns for request [/Demo/test.html] are [/Demo/*]
3. URI Template variables for request [/Demo/test.html] are {}
4. Mapping [/Demo/test.html] to HandlerExecutionChain with handler [st.controller.DemoController@1ed1dbe] and 1 interceptor
5. Returning handler method name 'test' for lookup path: /Demo/test.html
6. Last-Modified value for [/demo/Demo/test.html] is: -1
7. =====preHandle====
8. =====preHandle2====
9. Returning handler method name 'test' for lookup path: /Demo/test.html
10. 进入到demo的test方法
11. ==========postHandle2=========
12. view name : /Demo/test
13. ==========postHandle=========
14. view name : /Demo/test
15. Invoking afterPropertiesSet() on bean with name '/Demo/test'
springmvc常用标签16. Rendering view [org.springframework.web.servlet.view.JstlView: name '/Demo/test'; URL [/Demo/test.jsp]] in DispatcherServlet with name 'springmvc'
17. Forwarding to resource [/Demo/test.jsp] in InternalResourceView '/Demo/test'
18. =====afterCompletion2====
19. =====afterCompletion====
20. Successfully completed request
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论