spring-webmvc-4.3.14.RELEASE.jar
org.springframework.web.servlet.DispatcherServlet#doDispatch
/** * Process the actual dispatching to the handler. * <p>The handler will be obtained by applying the servlet's HandlerMappings in order. * The HandlerAdapter will be obtained by querying the servlet's installed HandlerAdapters * to find the first that supports the handler class. * <p>All HTTP methods are handled by this method. It's up to HandlerAdapters or handlers * themselves to decide which methods are acceptable. * @param request current HTTP request * @param response current HTTP response * @throws Exception in case of any kind of processing failure */ protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception { HttpServletRequest processedRequest = request; HandlerExecutionChain mappedHandler = null; boolean multipartRequestParsed = false; WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request); try { ModelAndView mv = null; Exception dispatchException = null; try { processedRequest = checkMultipart(request); multipartRequestParsed = (processedRequest != request); // Determine handler for the current request. mappedHandler = getHandler(processedRequest); if (mappedHandler == null || mappedHandler.getHandler() == null) { noHandlerFound(processedRequest, response); return; } // Determine handler adapter for the current request. HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler()); // Process last-modified header, if supported by the handler. String method = request.getMethod(); boolean isGet = "GET".equals(method); if (isGet || "HEAD".equals(method)) { long lastModified = ha.getLastModified(request, mappedHandler.getHandler()); if (logger.isDebugEnabled()) { logger.debug("Last-Modified value for [" + getRequestUri(request) + "] is: " + lastModified); } if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) { return; } } if (!mappedHandler.applyPreHandle(processedRequest, response)) { return; } // Actually invoke the handler. mv = ha.handle(processedRequest, response, mappedHandler.getHandler()); if (asyncManager.isConcurrentHandlingStarted()) { return; } applyDefaultViewName(processedRequest, mv); mappedHandler.applyPostHandle(processedRequest, response, mv); } catch (Exception ex) { dispatchException = ex; } catch (Throwable err) { // As of 4.3, we're processing Errors thrown from handler methods as well, // making them available for @ExceptionHandler methods and other scenarios. dispatchException = new NestedServletException("Handler dispatch failed", err); } processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException); } catch (Exception ex) { triggerAfterCompletion(processedRequest, response, mappedHandler, ex); } catch (Throwable err) { triggerAfterCompletion(processedRequest, response, mappedHandler, new NestedServletException("Handler processing failed", err)); } finally { if (asyncManager.isConcurrentHandlingStarted()) { // Instead of postHandle and afterCompletion if (mappedHandler != null) { mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response); } } else { // Clean up any resources used by a multipart request. if (multipartRequestParsed) { cleanupMultipart(processedRequest); } } } }
拦截器的preHandle
if (!mappedHandler.applyPreHandle(processedRequest, response)) { return; }
拦截器的postHandle
mappedHandler.applyPostHandle(processedRequest, response, mv);
拦截器的afterCompletion
finally { if (asyncManager.isConcurrentHandlingStarted()) { // Instead of postHandle and afterCompletion if (mappedHandler != null) { mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response); } }
这三个方法的执行流程:
发起请求,进入拦截器链,运行所有拦截器的preHandle方法.
当preHandle方法返回false时,(后面的拦截器就不再执行了)从当前拦截器往回执行所有拦截器的afterCompletion方法(当前这个拦截器的afterCompletion不会被执行。此时所有的preHandle和postHandle都执行完毕),再退出拦截器链。
当preHandle方法全为true时,执行下一个拦截器的preHandle,直到所有拦截器的preHandle执行完。
再运行被拦截的Controller。然后进入拦截器链,运行所有拦截器的postHandle方法(执行顺序与preHandle的顺序相反。在postHandle中,有机会修改ModelAndView),
完后从最后一个拦截器往回执行所有拦截器的afterCompletion方法(执行顺序与preHandle的顺序相反,与postHandle相同。用于清理资源).
当有拦截器抛出异常时,会从当前拦截器往回执行所有拦截器的afterCompletion方法
preHandle方法:
返回true,映射处理器执行链将继续执行;
当返回false时,DispatcherServlet处理器认为拦截器已经处理完了请求(这个请求已经被拦截了:http请求的status是200,但response什么也没有),而不继续执行执行链中的其它拦截器和处理器。
实现org.springframework.web.servlet.HandlerInterceptor接口的拦截器:
MyInterceptor1
MyInterceptor2
MyInterceptor3
package com.tangcheng.learning.web.interceptor; import lombok.extern.slf4j.Slf4j; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @author tangcheng * 2018/03/06 */ @Slf4j public class MyInterceptor1 implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { log.info(">>>MyInterceptor1>>>>>>>preHandle在请求处理之前进行调用(Controller方法调用之前)"); return true;// 只有返回true才会继续向下执行,返回false取消当前请求 } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { log.info(">>>MyInterceptor1>>>>>>>postHandle请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)."); if (modelAndView != null) { log.info("ViewName:{}", modelAndView.getViewName()); } } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { log.info(">>>MyInterceptor1>>>>>>>afterCompletion.在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)"); } }
package com.tangcheng.learning.web.interceptor; import lombok.extern.slf4j.Slf4j; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @author tangcheng * 2018/03/06 */ @Slf4j public class MyInterceptor2 implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { log.info(">>>MyInterceptor2>>>>>>>preHandle在请求处理之前进行调用(Controller方法调用之前)"); return true;// 只有返回true才会继续向下执行,返回false取消当前请求 } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { log.info(">>>MyInterceptor2>>>>>>>postHandle请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)."); if (modelAndView != null) { log.info("ViewName:{}", modelAndView.getViewName()); } } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { log.info(">>>MyInterceptor2>>>>>>>afterCompletion.在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)"); } }
package com.tangcheng.learning.web.interceptor; import lombok.extern.slf4j.Slf4j; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @author tangcheng * 2018/03/06 */ @Slf4j public class MyInterceptor3 implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { log.info(">>>MyInterceptor3>>>>>>>preHandle在请求处理之前进行调用(Controller方法调用之前)"); return true;// 只有返回true才会继续向下执行,返回false取消当前请求 } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { log.info(">>>MyInterceptor3>>>>>>>postHandle请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)."); if (modelAndView != null) { log.info("ViewName:{}", modelAndView.getViewName()); } } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { log.info(">>>MyInterceptor3>>>>>>>afterCompletion.在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)"); } }
Spring boot下的配置:
package com.tangcheng.learning.web.config; import com.tangcheng.learning.web.interceptor.MyInterceptor1; import com.tangcheng.learning.web.interceptor.MyInterceptor2; import com.tangcheng.learning.web.interceptor.MyInterceptor3; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; /** * @author tangcheng * 2018/03/06 */ @Configuration public class WebConfig extends WebMvcConfigurerAdapter { /** * MyInterceptor2中的preHandle返回false如下执行情况。并且相关rest请求页面不会正常响应,即页面会白屏 * c.t.learning.interceptor.MyInterceptor1 : >>>MyInterceptor1>>>>>>>preHandle在请求处理之前进行调用(Controller方法调用之前) * c.t.learning.interceptor.MyInterceptor2 : >>>MyInterceptor2>>>>>>>preHandle在请求处理之前进行调用(Controller方法调用之前) * c.t.learning.interceptor.MyInterceptor1 : >>>MyInterceptor1>>>>>>>afterCompletion.在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作) * * @param registry */ @Override public void addInterceptors(InterceptorRegistry registry) { super.addInterceptors(registry); registry.addInterceptor(new MyInterceptor1()); registry.addInterceptor(new MyInterceptor2()); registry.addInterceptor(new MyInterceptor3()); } }
Controller层代码:
package com.tangcheng.learning.web.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * @author tangcheng * 2018/03/06 */ @Controller public class IndexController { @RequestMapping("/") public String index() { return "index"; } }
报错时的执行日志:
有个奇怪的现象,拦截器会被同一线程连续调用两次,为什么呢?【升级Spring Boot版本从1.5.9.RELEASE到1.5.10.RELEASE后连续调用两次的问题消失了.Tomcat版本升级到了8.5】
2018-03-06 11:21:45.342 INFO 18208 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet' 2018-03-06 11:21:45.343 INFO 18208 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started 2018-03-06 11:21:45.358 INFO 18208 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 15 ms 2018-03-06 11:21:45.366 INFO 18208 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor1 : >>>MyInterceptor1>>>>>>>preHandle在请求处理之前进行调用(Controller方法调用之前) 2018-03-06 11:21:45.366 INFO 18208 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor2 : >>>MyInterceptor2>>>>>>>preHandle在请求处理之前进行调用(Controller方法调用之前) 2018-03-06 11:21:45.366 INFO 18208 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor3 : >>>MyInterceptor3>>>>>>>preHandle在请求处理之前进行调用(Controller方法调用之前) 2018-03-06 11:21:45.372 INFO 18208 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor3 : >>>MyInterceptor3>>>>>>>postHandle请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后). 2018-03-06 11:21:45.372 INFO 18208 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor3 : ViewName:index 2018-03-06 11:21:45.372 INFO 18208 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor2 : >>>MyInterceptor2>>>>>>>postHandle请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后). 2018-03-06 11:21:45.372 INFO 18208 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor2 : ViewName:index 2018-03-06 11:21:45.372 INFO 18208 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor1 : >>>MyInterceptor1>>>>>>>postHandle请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后). 2018-03-06 11:21:45.372 INFO 18208 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor1 : ViewName:index 2018-03-06 11:21:45.377 INFO 18208 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor1 : >>>MyInterceptor1>>>>>>>preHandle在请求处理之前进行调用(Controller方法调用之前) 2018-03-06 11:21:45.377 INFO 18208 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor2 : >>>MyInterceptor2>>>>>>>preHandle在请求处理之前进行调用(Controller方法调用之前) 2018-03-06 11:21:45.377 INFO 18208 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor3 : >>>MyInterceptor3>>>>>>>preHandle在请求处理之前进行调用(Controller方法调用之前) 2018-03-06 11:21:45.377 INFO 18208 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor3 : >>>MyInterceptor3>>>>>>>postHandle请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后). 2018-03-06 11:21:45.377 INFO 18208 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor3 : ViewName:index 2018-03-06 11:21:45.377 INFO 18208 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor2 : >>>MyInterceptor2>>>>>>>postHandle请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后). 2018-03-06 11:21:45.377 INFO 18208 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor2 : ViewName:index 2018-03-06 11:21:45.377 INFO 18208 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor1 : >>>MyInterceptor1>>>>>>>postHandle请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后). 2018-03-06 11:21:45.377 INFO 18208 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor1 : ViewName:index 2018-03-06 11:21:45.377 INFO 18208 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor3 : >>>MyInterceptor3>>>>>>>afterCompletion.在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作) 2018-03-06 11:21:45.377 INFO 18208 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor2 : >>>MyInterceptor2>>>>>>>afterCompletion.在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作) 2018-03-06 11:21:45.377 INFO 18208 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor1 : >>>MyInterceptor1>>>>>>>afterCompletion.在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作) 2018-03-06 11:21:45.381 ERROR 18208 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] threw exception javax.servlet.ServletException: Circular view path [index]: would dispatch back to the current handler URL [/index] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.) at org.springframework.web.servlet.view.InternalResourceView.prepareForRendering(InternalResourceView.java:205) [spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:145) [spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303) [spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1286) [spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1041) [spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:984) [spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) [spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) [spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) [spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) [spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat-embed-websocket-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101) [spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101) [spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101) [spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101) [spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:728) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:467) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:392) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:311) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168) [spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303) [spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1286) [spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1041) [spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:984) [spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) [spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) [spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) [spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) [spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat-embed-websocket-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) [spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:108) [spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) [spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) [spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.23.jar:8.5.23] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_144] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_144] at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.23.jar:8.5.23] at java.lang.Thread.run(Thread.java:748) [na:1.8.0_144] 2018-03-06 11:21:45.385 INFO 18208 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor3 : >>>MyInterceptor3>>>>>>>afterCompletion.在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作) 2018-03-06 11:21:45.385 INFO 18208 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor2 : >>>MyInterceptor2>>>>>>>afterCompletion.在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作) 2018-03-06 11:21:45.385 INFO 18208 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor1 : >>>MyInterceptor1>>>>>>>afterCompletion.在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作) 2018-03-06 11:21:45.385 ERROR 18208 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Circular view path [index]: would dispatch back to the current handler URL [/index] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)] with root cause javax.servlet.ServletException: Circular view path [index]: would dispatch back to the current handler URL [/index] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.) at org.springframework.web.servlet.view.InternalResourceView.prepareForRendering(InternalResourceView.java:205) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:145) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1286) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1041) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:984) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:728) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:467) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:392) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:311) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1286) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1041) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:984) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:108) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) ~[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.23.jar:8.5.23] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_144] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_144] at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.23.jar:8.5.23] at java.lang.Thread.run(Thread.java:748) [na:1.8.0_144] 2018-03-06 11:21:45.389 INFO 18208 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor1 : >>>MyInterceptor1>>>>>>>preHandle在请求处理之前进行调用(Controller方法调用之前) 2018-03-06 11:21:45.389 INFO 18208 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor2 : >>>MyInterceptor2>>>>>>>preHandle在请求处理之前进行调用(Controller方法调用之前) 2018-03-06 11:21:45.389 INFO 18208 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor3 : >>>MyInterceptor3>>>>>>>preHandle在请求处理之前进行调用(Controller方法调用之前) 2018-03-06 11:21:45.420 INFO 18208 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor3 : >>>MyInterceptor3>>>>>>>postHandle请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后). 2018-03-06 11:21:45.420 INFO 18208 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor2 : >>>MyInterceptor2>>>>>>>postHandle请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后). 2018-03-06 11:21:45.420 INFO 18208 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor1 : >>>MyInterceptor1>>>>>>>postHandle请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后). 2018-03-06 11:21:45.420 INFO 18208 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor3 : >>>MyInterceptor3>>>>>>>afterCompletion.在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作) 2018-03-06 11:21:45.420 INFO 18208 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor2 : >>>MyInterceptor2>>>>>>>afterCompletion.在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作) 2018-03-06 11:21:45.420 INFO 18208 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor1 : >>>MyInterceptor1>>>>>>>afterCompletion.在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)
升级到Spring Boot1.5.10.RELEASE后的日志打印情况:
2018-03-06 18:26:59.988 INFO 23388 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet' 2018-03-06 18:26:59.988 INFO 23388 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started 2018-03-06 18:27:00.003 INFO 23388 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 14 ms 2018-03-06 18:27:00.013 INFO 23388 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor1 : >>>MyInterceptor1>>>>>>>preHandle在请求处理之前进行调用(Controller方法调用之前) 2018-03-06 18:27:00.013 INFO 23388 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor2 : >>>MyInterceptor2>>>>>>>preHandle在请求处理之前进行调用(Controller方法调用之前) 2018-03-06 18:27:00.013 INFO 23388 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor3 : >>>MyInterceptor3>>>>>>>preHandle在请求处理之前进行调用(Controller方法调用之前) 2018-03-06 18:27:00.019 INFO 23388 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor3 : >>>MyInterceptor3>>>>>>>postHandle请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后). 2018-03-06 18:27:00.020 INFO 23388 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor3 : ViewName:index 2018-03-06 18:27:00.020 INFO 23388 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor2 : >>>MyInterceptor2>>>>>>>postHandle请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后). 2018-03-06 18:27:00.020 INFO 23388 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor2 : ViewName:index 2018-03-06 18:27:00.020 INFO 23388 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor1 : >>>MyInterceptor1>>>>>>>postHandle请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后). 2018-03-06 18:27:00.020 INFO 23388 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor1 : ViewName:index 2018-03-06 18:27:00.029 INFO 23388 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor3 : >>>MyInterceptor3>>>>>>>afterCompletion.在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作) 2018-03-06 18:27:00.029 INFO 23388 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor2 : >>>MyInterceptor2>>>>>>>afterCompletion.在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作) 2018-03-06 18:27:00.029 INFO 23388 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor1 : >>>MyInterceptor1>>>>>>>afterCompletion.在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作) 2018-03-06 18:27:00.031 INFO 23388 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor1 : >>>MyInterceptor1>>>>>>>preHandle在请求处理之前进行调用(Controller方法调用之前) 2018-03-06 18:27:00.031 INFO 23388 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor2 : >>>MyInterceptor2>>>>>>>preHandle在请求处理之前进行调用(Controller方法调用之前) 2018-03-06 18:27:00.031 INFO 23388 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor3 : >>>MyInterceptor3>>>>>>>preHandle在请求处理之前进行调用(Controller方法调用之前) 2018-03-06 18:27:00.037 INFO 23388 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor3 : >>>MyInterceptor3>>>>>>>postHandle请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后). 2018-03-06 18:27:00.037 INFO 23388 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor3 : ViewName:error 2018-03-06 18:27:00.037 INFO 23388 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor2 : >>>MyInterceptor2>>>>>>>postHandle请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后). 2018-03-06 18:27:00.037 INFO 23388 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor2 : ViewName:error 2018-03-06 18:27:00.037 INFO 23388 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor1 : >>>MyInterceptor1>>>>>>>postHandle请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后). 2018-03-06 18:27:00.037 INFO 23388 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor1 : ViewName:error 2018-03-06 18:27:00.046 INFO 23388 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor3 : >>>MyInterceptor3>>>>>>>afterCompletion.在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作) 2018-03-06 18:27:00.046 INFO 23388 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor2 : >>>MyInterceptor2>>>>>>>afterCompletion.在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作) 2018-03-06 18:27:00.046 INFO 23388 --- [nio-8080-exec-1] c.t.l.web.interceptor.MyInterceptor1 : >>>MyInterceptor1>>>>>>>afterCompletion.在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)
spring mvc DispatcherServlet详解之interceptor和filter的区别
http://www.cnblogs.com/softidea/p/7426072.html
Spring Boot实现一个监听用户请求的拦截器
http://www.cnblogs.com/softidea/p/5987764.html
1.配置拦截器
在springMVC.xml配置文件增加:
<mvc:interceptors>
<!-- 日志拦截器 -->
<mvc:interceptor>
<mvc:mapping path="/**" />
<mvc:exclude-mapping path="/static/**" />
<bean class="拦截器java代码路径" />
</mvc:interceptor>
</mvc:interceptors>
说明:
1)mvc:mapping 拦截器路径配置
2)mvc:exclude-mapping 拦截器不需要拦截的路径
public class WebMvcConfig extends WebMvcConfigurerAdapter { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(userLoginInterceptor()) .addPathPatterns("/**") .excludePathPatterns( "/robots.txt"); }
http://www.68idc.cn/help/buildlang/ask/20150424325278.html
Since Spring 3, Java configuration (@Configuration) has been moved into spring-core and has caught my attention. This is a quick sample of how to convert an existing servlet.xml file into a java config file extending WebMvcConfigurerAdapter.
Beginning xml
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
<? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:mvc = "http://www.springframework.org/schema/mvc" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:context = "http://www.springframework.org/schema/context" xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd" > <!-- Scan for spring annotated components --> < context:component-scan base-package = "com.luckyryan.sample" /> <!-- Process annotations on registered beans like @Autowired... --> < context:annotation-config /> <!-- This tag registers the DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter beans that are required for Spring MVC --> < mvc:annotation-driven /> <!-- Exception Resolver that resolves exceptions through @ExceptionHandler methods --> < bean class = "org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver" /> <!-- View Resolver for JSPs --> < bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" > < property name = "prefix" value = "/WEB-INF/pages/" /> < property name = "suffix" value = ".jsp" /> </ bean > <!-- This tag allows for mapping the DispatcherServlet to "/" --> < mvc:default-servlet-handler /> <!-- resources exclusions from servlet mapping --> < mvc:resources mapping = "/assets/**" location = "classpath:/META-INF/resources/webjars/" /> < mvc:resources mapping = "/css/**" location = "/css/" /> < mvc:resources mapping = "/img/**" location = "/img/" /> < mvc:resources mapping = "/js/**" location = "/js/" /> </ beans > |
1. Create the configuration class, I like to create a “config” package with a class named appConfig.java
2. Add @Configuration, this will let spring know this contains bean definitions.
01
02
03
04
05
06
07
08
|
package com.luckyryan.sample.config; import org.springframework.context.annotation.Configuration; @Configuration public class appConfig { } |
3. Add @EnableWebMVC, this is the same as <mvc:annotation-driven/>
01
02
03
04
05
|
@EnableWebMvc @Configuration public class appConfig { } |
4. Add @ComponentScan(basePackages = {“com.luckyryan.sample”}), this is the same as <context:component-scan base-package=”com.luckyryan.sample”/>
01
02
03
04
05
06
|
@EnableWebMvc @ComponentScan (basePackages = { "com.luckyryan.sample" }) @Configuration public class appConfig { } |
5. Extend the class to use WebMvcConfigurerAdapter. This adds stub implementations from the WebMvcConfigurer interface which is used by @EnableWebMVC. It also gives us a chance to override resources and the default handler.
01
02
03
04
05
06
|
@EnableWebMvc @ComponentScan (basePackages = { "com.luckyryan.sample" }) @Configuration public class appConfig extends WebMvcConfigurerAdapter { } |
6. Declare our static resources. I added cache to the java config but it’s not required.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
|
@EnableWebMvc @ComponentScan (basePackages = { "com.luckyryan.sample" }) @Configuration public class appConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler( "/assets/**" ).addResourceLocations( "classpath:/META-INF/resources/webjars/" ).setCachePeriod( 31556926 ); registry.addResourceHandler( "/css/**" ).addResourceLocations( "/css/" ).setCachePeriod( 31556926 ); registry.addResourceHandler( "/img/**" ).addResourceLocations( "/img/" ).setCachePeriod( 31556926 ); registry.addResourceHandler( "/js/**" ).addResourceLocations( "/js/" ).setCachePeriod( 31556926 ); } } |
7. Set default servlet handler, this is the same as <mvc:default-servlet-handler/>
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
@EnableWebMvc @ComponentScan (basePackages = { "com.luckyryan.sample" }) @Configuration public class appConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler( "/assets/**" ).addResourceLocations( "classpath:/META-INF/resources/webjars/" ).setCachePeriod( 31556926 ); registry.addResourceHandler( "/css/**" ).addResourceLocations( "/css/" ).setCachePeriod( 31556926 ); registry.addResourceHandler( "/img/**" ).addResourceLocations( "/img/" ).setCachePeriod( 31556926 ); registry.addResourceHandler( "/js/**" ).addResourceLocations( "/js/" ).setCachePeriod( 31556926 ); } @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } @Bean public InternalResourceViewResolver getInternalResourceViewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix( "/WEB-INF/pages/" ); resolver.setSuffix( ".jsp" ); return resolver; } } |
8. Add bean for InternalResourceViewResolver
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
package com.luckyryan.sample.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.InternalResourceViewResolver; @EnableWebMvc @ComponentScan (basePackages = { "com.luckyryan.sample" }) @Configuration public class appConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler( "/assets/**" ).addResourceLocations( "classpath:/META-INF/resources/webjars/" ).setCachePeriod( 31556926 ); registry.addResourceHandler( "/css/**" ).addResourceLocations( "/css/" ).setCachePeriod( 31556926 ); registry.addResourceHandler( "/img/**" ).addResourceLocations( "/img/" ).setCachePeriod( 31556926 ); registry.addResourceHandler( "/js/**" ).addResourceLocations( "/js/" ).setCachePeriod( 31556926 ); } @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } @Bean public InternalResourceViewResolver getInternalResourceViewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix( "/WEB-INF/pages/" ); resolver.setSuffix( ".jsp" ); return resolver; } } |
9. Update the servlet declaration in web.xml for the new config class and annotation conf. Note: this replaces the need for <context:annotation-config/> which was not featured in the servlet.xml example.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
... ... < servlet > < servlet-name >sample</ servlet-name > < servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class > < init-param > < param-name >contextClass</ param-name > < param-value > org.springframework.web.context.support.AnnotationConfigWebApplicationContext </ param-value > </ init-param > < init-param > < param-name >contextConfigLocation</ param-name > < param-value > com.luckyryan.sample.config.appConfig </ param-value > </ init-param > </ servlet > < servlet-mapping > < servlet-name >sample</ servlet-name > < url-pattern >/</ url-pattern > </ servlet-mapping > |
I am at a loss moving from Spring XML to Java-config
http://stackoverflow.com/questions/15438835/i-am-at-a-loss-moving-from-spring-xml-to-java-config
SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理。比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那样子判断当前时间是否是购票时间。
一、定义Interceptor实现类
SpringMVC 中的Interceptor 拦截请求是通过HandlerInterceptor 来实现的。在SpringMVC 中定义一个Interceptor 非常简单,主要有两种方式,第一种方式是要定义的Interceptor类要实现了Spring 的HandlerInterceptor 接口,或者是这个类继承实现了HandlerInterceptor 接口的类,比如Spring 已经提供的实现了HandlerInterceptor 接口的抽象类HandlerInterceptorAdapter ;第二种方式是实现Spring的WebRequestInterceptor接口,或者是继承实现了WebRequestInterceptor的类。
(一)实现HandlerInterceptor接口
HandlerInterceptor 接口中定义了三个方法,我们就是通过这三个方法来对用户的请求进行拦截处理的。
(1 )preHandle (HttpServletRequest request, HttpServletResponse response, Object handle) 方法,顾名思义,该方法将在请求处理之前进行调用。SpringMVC 中的Interceptor 是链式的调用的,在一个应用中或者说是在一个请求中可以同时存在多个Interceptor 。每个Interceptor 的调用会依据它的声明顺序依次执行,而且最先执行的都是Interceptor 中的preHandle 方法,所以可以在这个方法中进行一些前置初始化操作或者是对当前请求的一个预处理,也可以在这个方法中进行一些判断来决定请求是否要继续进行下去。该方法的返回值是布尔值Boolean 类型的,当它返回为false 时,表示请求结束,后续的Interceptor 和Controller 都不会再执行;当返回值为true 时就会继续调用下一个Interceptor 的preHandle 方法,如果已经是最后一个Interceptor 的时候就会是调用当前请求的Controller 方法。
(2 )postHandle (HttpServletRequest request, HttpServletResponse response, Object handle, ModelAndView modelAndView) 方法,由preHandle 方法的解释我们知道这个方法包括后面要说到的afterCompletion 方法都只能是在当前所属的Interceptor 的preHandle 方法的返回值为true 时才能被调用。postHandle 方法,顾名思义就是在当前请求进行处理之后,也就是Controller 方法调用之后执行,但是它会在DispatcherServlet 进行视图返回渲染之前被调用,所以我们可以在这个方法中对Controller 处理之后的ModelAndView 对象进行操作。postHandle 方法被调用的方向跟preHandle 是相反的,也就是说先声明的Interceptor 的postHandle 方法反而会后执行,这和Struts2 里面的Interceptor 的执行过程有点类型。Struts2 里面的Interceptor 的执行过程也是链式的,只是在Struts2 里面需要手动调用ActionInvocation 的invoke 方法来触发对下一个Interceptor 或者是Action 的调用,然后每一个Interceptor 中在invoke 方法调用之前的内容都是按照声明顺序执行的,而invoke 方法之后的内容就是反向的。
(3 )afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handle, Exception ex) 方法,该方法也是需要当前对应的Interceptor 的preHandle 方法的返回值为true 时才会执行。顾名思义,该方法将在整个请求结束之后,也就是在DispatcherServlet 渲染了对应的视图之后执行。这个方法的主要作用是用于进行资源清理工作的。
下面是一个简单的代码说明:
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.springframework.web.servlet.HandlerInterceptor;
- import org.springframework.web.servlet.ModelAndView;
- public class SpringMVCInterceptor implements HandlerInterceptor {
- /**
- * preHandle方法是进行处理器拦截用的,顾名思义,该方法将在Controller处理之前进行调用,SpringMVC中的Interceptor拦截器是链式的,可以同时存在
- * 多个Interceptor,然后SpringMVC会根据声明的前后顺序一个接一个的执行,而且所有的Interceptor中的preHandle方法都会在
- * Controller方法调用之前调用。SpringMVC的这种Interceptor链式结构也是可以进行中断的,这种中断方式是令preHandle的返
- * 回值为false,当preHandle的返回值为false的时候整个请求就结束了。
- */
- @Override
- public boolean preHandle(HttpServletRequest request,
- HttpServletResponse response, Object handler) throws Exception {
- // TODO Auto-generated method stub
- return false;
- }
- /**
- * 这个方法只会在当前这个Interceptor的preHandle方法返回值为true的时候才会执行。postHandle是进行处理器拦截用的,它的执行时间是在处理器进行处理之
- * 后,也就是在Controller的方法调用之后执行,但是它会在DispatcherServlet进行视图的渲染之前执行,也就是说在这个方法中你可以对ModelAndView进行操
- * 作。这个方法的链式结构跟正常访问的方向是相反的,也就是说先声明的Interceptor拦截器该方法反而会后调用,这跟Struts2里面的拦截器的执行过程有点像,
- * 只是Struts2里面的intercept方法中要手动的调用ActionInvocation的invoke方法,Struts2中调用ActionInvocation的invoke方法就是调用下一个Interceptor
- * 或者是调用action,然后要在Interceptor之前调用的内容都写在调用invoke之前,要在Interceptor之后调用的内容都写在调用invoke方法之后。
- */
- @Override
- public void postHandle(HttpServletRequest request,
- HttpServletResponse response, Object handler,
- ModelAndView modelAndView) throws Exception {
- // TODO Auto-generated method stub
- }
- /**
- * 该方法也是需要当前对应的Interceptor的preHandle方法的返回值为true时才会执行。该方法将在整个请求完成之后,也就是DispatcherServlet渲染了视图执行,
- * 这个方法的主要作用是用于清理资源的,当然这个方法也只能在当前这个Interceptor的preHandle方法的返回值为true时才会执行。
- */
- @Override
- public void afterCompletion(HttpServletRequest request,
- HttpServletResponse response, Object handler, Exception ex)
- throws Exception {
- // TODO Auto-generated method stub
- }
- }
(二)实现WebRequestInterceptor 接口
WebRequestInterceptor 中也定义了三个方法,我们也是通过这三个方法来实现拦截的。这三个方法都传递了同一个参数WebRequest ,那么这个WebRequest 是什么呢?这个WebRequest 是Spring 定义的一个接口,它里面的方法定义都基本跟HttpServletRequest 一样,在WebRequestInterceptor 中对WebRequest 进行的所有操作都将同步到HttpServletRequest 中,然后在当前请求中一直传递。
(1 )preHandle(WebRequest request) 方法。该方法将在请求处理之前进行调用,也就是说会在Controller 方法调用之前被调用。这个方法跟HandlerInterceptor 中的preHandle 是不同的,主要区别在于该方法的返回值是void ,也就是没有返回值,所以我们一般主要用它来进行资源的准备工作,比如我们在使用Hibernate 的时候可以在这个方法中准备一个Hibernate 的Session 对象,然后利用WebRequest 的setAttribute(name, value, scope) 把它放到WebRequest 的属性中。这里可以说说这个setAttribute 方法的第三个参数scope ,该参数是一个Integer 类型的。在WebRequest 的父层接口RequestAttributes 中对它定义了三个常量:
SCOPE_REQUEST :它的值是0 ,代表只有在request 中可以访问。
SCOPE_SESSION :它的值是1 ,如果环境允许的话它代表的是一个局部的隔离的session,否则就代表普通的session,并且在该session范围内可以访问。
SCOPE_GLOBAL_SESSION :它的值是2 ,如果环境允许的话,它代表的是一个全局共享的session,否则就代表普通的session,并且在该session 范围内可以访问。
(2 )postHandle(WebRequest request, ModelMap model) 方法。该方法将在请求处理之后,也就是在Controller 方法调用之后被调用,但是会在视图返回被渲染之前被调用,所以可以在这个方法里面通过改变数据模型ModelMap 来改变数据的展示。该方法有两个参数,WebRequest 对象是用于传递整个请求数据的,比如在preHandle 中准备的数据都可以通过WebRequest 来传递和访问;ModelMap 就是Controller 处理之后返回的Model 对象,我们可以通过改变它的属性来改变返回的Model 模型。
(3 )afterCompletion(WebRequest request, Exception ex) 方法。该方法会在整个请求处理完成,也就是在视图返回并被渲染之后执行。所以在该方法中可以进行资源的释放操作。而WebRequest 参数就可以把我们在preHandle 中准备的资源传递到这里进行释放。Exception 参数表示的是当前请求的异常对象,如果在Controller 中抛出的异常已经被Spring 的异常处理器给处理了的话,那么这个异常对象就是是null 。
下面是一个简单的代码说明:
- import org.springframework.ui.ModelMap;
- import org.springframework.web.context.request.WebRequest;
- import org.springframework.web.context.request.WebRequestInterceptor;
- public class AllInterceptor implements WebRequestInterceptor {
- /**
- * 在请求处理之前执行,该方法主要是用于准备资源数据的,然后可以把它们当做请求属性放到WebRequest中
- */
- @Override
- public void preHandle(WebRequest request) throws Exception {
- // TODO Auto-generated method stub
- System.out.println("AllInterceptor...............................");
- request.setAttribute("request", "request", WebRequest.SCOPE_REQUEST);//这个是放到request范围内的,所以只能在当前请求中的request中获取到
- request.setAttribute("session", "session", WebRequest.SCOPE_SESSION);//这个是放到session范围内的,如果环境允许的话它只能在局部的隔离的会话中访问,否则就是在普通的当前会话中可以访问
- request.setAttribute("globalSession", "globalSession", WebRequest.SCOPE_GLOBAL_SESSION);//如果环境允许的话,它能在全局共享的会话中访问,否则就是在普通的当前会话中访问
- }
- /**
- * 该方法将在Controller执行之后,返回视图之前执行,ModelMap表示请求Controller处理之后返回的Model对象,所以可以在
- * 这个方法中修改ModelMap的属性,从而达到改变返回的模型的效果。
- */
- @Override
- public void postHandle(WebRequest request, ModelMap map) throws Exception {
- // TODO Auto-generated method stub
- for (String key:map.keySet())
- System.out.println(key + "-------------------------");;
- map.put("name3", "value3");
- map.put("name1", "name1");
- }
- /**
- * 该方法将在整个请求完成之后,也就是说在视图渲染之后进行调用,主要用于进行一些资源的释放
- */
- @Override
- public void afterCompletion(WebRequest request, Exception exception)
- throws Exception {
- // TODO Auto-generated method stub
- System.out.println(exception + "-=-=--=--=-=-=-=-=-=-=-=-==-=--=-=-=-=");
- }
- }
二、把定义的拦截器类加到SpringMVC的拦截体系中
1.在SpringMVC的配置文件中加上支持MVC的schema
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation=" http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
下面是我的声明示例:
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
这样在SpringMVC的配置文件中就可以使用mvc标签了,mvc标签中有一个mvc:interceptors是用于声明SpringMVC的拦截器的。
(二)使用mvc:interceptors标签来声明需要加入到SpringMVC拦截器链中的拦截器
- <mvc:interceptors>
- <!-- 使用bean定义一个Interceptor,直接定义在mvc:interceptors根下面的Interceptor将拦截所有的请求 -->
- <bean class="com.host.app.web.interceptor.AllInterceptor"/>
- <mvc:interceptor>
- <mvc:mapping path="/test/number.do"/>
- <!-- 定义在mvc:interceptor下面的表示是对特定的请求才进行拦截的 -->
- <bean class="com.host.app.web.interceptor.LoginInterceptor"/>
- </mvc:interceptor>
- </mvc:interceptors>
由上面的示例可以看出可以利用mvc:interceptors标签声明一系列的拦截器,然后它们就可以形成一个拦截器链,拦截器的执行顺序是按声明的先后顺序执行的,先声明的拦截器中的preHandle方法会先执行,然而它的postHandle方法和afterCompletion方法却会后执行。
在mvc:interceptors标签下声明interceptor主要有两种方式:
(1)直接定义一个Interceptor实现类的bean对象。使用这种方式声明的Interceptor拦截器将会对所有的请求进行拦截。
(2)使用mvc:interceptor标签进行声明。使用这种方式进行声明的Interceptor可以通过mvc:mapping子标签来定义需要进行拦截的请求路径。
经过上述两步之后,定义的拦截器就会发生作用对特定的请求进行拦截了。
(三)使用JAVA代码配置Interceptor
配置spring mvc的拦截器WebMvcConfigurerAdapter
public class WebAppConfig extends WebMvcConfigurerAdapter
实现添加拦截器方法
public void addInterceptors(InterceptorRegistry registry){ } registry.addInterceptor可以通过此方法添加拦截器, 可以是spring提供的或者自己添加的
例子
public class WebAppConfig extends WebMvcConfigurerAdapter{ public static void main(String[] args) { SpringApplication.run(WebAppConfig.class, args); } /** * 配置拦截器 * @author lance * @param registry */ public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new UserSecurityInterceptor()).addPathPatterns("/user/**"); } } UserSecurityInterceptor代码 public class UserSecurityInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }
http://www.cnblogs.com/hyl8218/p/6056817.html