SpringMVC: interceptors interceptor usage

Source: Internet
Author: User

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 ");}}
  
 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.