Spring MVC interceptor and springmvc blocker

Source: Internet
Author: User

Spring MVC interceptor and springmvc blocker

Spring provides us:

Org. springframework. web. servlet.HandlerInterceptorInterface,

Org. springframework. web. servlet. handler.HandlerInterceptorAdapterAdapter,

You can easily implement your own interceptor by implementing this interface or inheriting this class.

For example:

public class HelloWorldInterceptor implements HandlerInterceptor  {  @Override  public boolean preHandle(HttpServletRequest request,          HttpServletResponse response, Object handler) throws Exception {            System.out.println("Pre-handle");            return false;  }    @Override  public void postHandle(HttpServletRequest request,          HttpServletResponse response, Object handler,          ModelAndView modelAndView) throws Exception {      System.out.println("Post-handle");  }    @Override  public void afterCompletion(HttpServletRequest request,          HttpServletResponse response, Object handler, Exception ex)          throws Exception {      System.out.println("After completion handle");  }  

There are three methods:

Execute before Action:

Public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler );
 
Run

Public void postHandle (HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView );
 
Last executed, which can be used to release resources

Public void afterCompletion (HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
 
Pre-processing and post-processing are implemented respectively (the Service is called and ModelAndView is returned, but no page rendering is performed) and the return processing is performed (the page has been rendered)

In preHandle, encoding and security control can be performed;

In postHandle, you have the opportunity to modify ModelAndView;

In afterCompletion, you can determine whether an exception has occurred based on whether ex is null and record the log.

The Object handler in the parameter is the next interceptor.

 

There are three methods to configure spring MVC configuration files:

Solution 1: (approximate) the total interceptor intercepts all URLs

If it is a REST-style URL, static resources will also be blocked.

<mvc:interceptors>      <bean class="com.app.mvc.MyInteceptor" />  </mvc:interceptors>  

Solution 2: intercept matching URLs.

If it is a REST-style URL, static resources will also be blocked.

<mvc:interceptors >      <mvc:interceptor>            <mvc:mapping path="/user/*" /> <!-- /user/*  -->            <bean class="com.mvc.MyInteceptor"></bean>        </mvc:interceptor>    </mvc:interceptors>    

Solution 3: Interceptor on HandlerMappint.

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">        <property name="interceptors">            <list>                <bean class="com.mvc.MyInteceptor"></bean>           </list>        </property>       </bean>  

If <mvc: annotation-driven/> is used, it automatically registers the bean ultannotationhandlermapping and AnnotationMethodHandlerAdapter beans, so there is no chance to inject the interceptors attribute to it, and the interceptor cannot be specified.

We can manually configure the preceding two beans without using <mvc: annotation-driven/> to inject the Interceptor to the interceptors attribute.

 


SpringMVC interceptor

ER · the answer is wrong.
Configure in web. xml
<! -- Add spring mvc -->
<Servlet>
<Servlet-name> spring </servlet-name> <! -- Here corresponds to your spring-servlet.xml -->
<Servlet-class> org. springframework. web. servlet. DispatcherServlet </servlet-class>
<! -- Pre-Controller: Define the Servlet-based interception and matching rules to intercept matching requests. Distribute the intercepted requests to the target Controller (the Action we wrote) based on the rules. -->
<Load-on-startup> 1 </load-on-startup>
<! -- The Starting sequence allows the Servlet to start with the Servletp container. -->
</Servlet>
<Servlet-mapping>
<Servlet-name> spring </servlet-name>
<! -- In the initialization process of DispatcherServlet, the framework will look for a configuration file named WEB-INF under the action-servlet.xml folder of the web application to generate the bean defined in the file. -->
<Url-pattern> *. do </url-pattern>
<! -- Intercepts the request ending with *. do -->

-------------------------------------------
Spring-servlet.xml configuration scan package, automatically match your Method

<! -- Scan the package -->
<Context: component-scan base-package = "foxking. action"/>

SpringMVC interceptor Problems

Mvc: exclude-mapping labels are not supported by the spring-mvc-3.0.xsd, which is in the spring-mvc-3.2.xsd and can be passed through
 

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.