SpringMVC: interceptors interceptor usage
1. Configure the interceptor
Add the following in the springMVC. xml configuration file:
Note:
1) mvc: mapping interceptor path Configuration
2) mvc: The exclude-mapping interceptor does not need to intercept the path
2. Reference Code
Public class LogsInterceptor extends HandlerInterceptorAdapter {private static final Logger logger = LoggerFactory. getLogger (LogsInterceptor. class); private NamedThreadLocal
LogContext = new NamedThreadLocal
("Log-id"); @ Autowired private TLogDao logDao;/*** the preHandle method is used for processor interception. As the name suggests, this method will be called before the Controller processes it, * The Interceptor in SpringMVC is chained and multiple interceptors can exist at the same time. * Then SpringMVC will execute the Interceptor one by one according to the declared order, * All the preHandle methods in Interceptor are called before the Controller method is called. * SpringMVC's Interceptor chain structure can also be interrupted. * This interrupt method sets the returned value of preHandle to false. When the returned value of preHandle is false, the entire request ends. * // @ Override public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {String host = request. getRemoteHost (); String url = request. getRequestURI (); TLogEntity entity = new TLogEntity (); entity. setCreateTime (new Timestamp (System. currentTimeMillis (); entity. setCreateUser ("admin"); entity. setIpAddress (host); entity. setLogUrl (url); entity. s EtIsSuccess ("N"); logDao. save (entity); logContext. set (entity. getLogId (); logger. debug ("IP ---- >>>" + host + "<----- access to the system"); return true ;} /*** this method will be executed only when the current Interceptor's preHandle method returns true. * PostHandle is used for processor interception. Its execution time is after processing by the processor, that is, after calling the Controller method, * However, it will be executed before DispatcherServlet renders the view. That is to say, you can operate ModelAndView in this method. * The chain structure of this method is opposite to that of normal access, that is, the first declared Interceptor is called after the method is called. * This is a bit like the execution process of the Interceptor in Struts2, * Only the intercept method in Struts2 needs to manually call the ActionInvocation invoke method. * The invoke method used to call ActionInvocation in Struts2 is to call the next Interceptor or call action, * The content to be called before Interceptor is written before invoke is called. The content to be called after Interceptor is written after invoke method is called. * // @ Override public void postHandle (HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {}/*** this method is executed only when the returned value of the current corresponding Interceptor preHandle method is true. * This method renders the view execution after the entire request is completed, that is, DispatcherServlet. This method is mainly used to clear resources. * // @ Override public void afterCompletion (HttpServletRequest request, httpServletResponse response, Object handler, Exception ex) {String host = request. getRemoteHost (); String logId = logContext. get (); TLogEntity entity = logDao. findOne (logId); entity. setIsSuccess ("Y"); logDao. save (entity); logger. debug ("IP ---- >>>" + host + "<----- access successful ");}}