SpringMVC: interceptors and POST Chinese garbage problem

SpringMVC: interceptors and POST Chinese garbage problem

1, interceptors

Spring MVC The interceptor (Interceptor) similar to the filter (Filter) Servlet is, it is mainly for intercepting a user request and processed accordingly.

The difference between the filter and the interceptor: Blocker is an application of AOP thought.

filter

  • Part of the servlet specification, any java web project can be used
  • After configuring / url-pattern in the *, can intercept all the resources you want to access

Interceptor

  • Interceptors are SpringMVC framework of their own, only to use the project to use the framework of SpringMVC
  • Interceptor will intercept access control method, if the visit is jsp / html / css / image / js will not be intercepted

Custom implementation interceptor:

By implementing HandlerInterceptorAdapterthe interface, and Spring MVC registered can use a custom interceptor.

HandlerInterceptorAdapter Interface provides three methods:

  • preHandle() Service processor is called before processing the request

    • This method will be executed before the Controller method
    • The return value indicates whether to continue the subsequent operations:
    • Return true, indicating that it goes;
    • Returns false, interrupts all subsequent operations (including call interception method and the next execution Controller).
  • postHandle() After the service processor performs processing request is completed, before performing generate views

    • After Controller method calls and parse executed before view.
    • This method can make further changes to the model and the view.
  • afterCompletion() In DispatcherServlet completely finished processing the request after calls, can be used to clean up resources.

    • This method is performed after completion (i.e., end view rendering) the entire request.
    • You can achieve some resource cleanup, logs and other information to work through this method.
public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return false;
    }

    @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 {

    }
}

Spring-web.xml configuration:

<mvc:interceptors>
  <bean class="com.xu.web.interceptor.MyInterceptor1"/> <!-- 1 -->

  <mvc:interceptor> <!-- 2 -->
    <mvc:mapping path="/**"/> <!-- 3 -->
    <mvc:exclude-mapping path=""/> <!-- 4 -->
    <bean class="com.xu.web.interceptor.MyInterceptor2"/> <!-- 5 -->
  </mvc:interceptor>

  <mvc:interceptor> <!-- 6 -->
    <mvc:mapping path="/hello"/> <!-- 7 -->
    <bean class="com.xu.web.interceptor.MyInterceptor3"/> <!-- 8 -->
  </mvc:interceptor>

</mvc:interceptors>
  • As 1shown, registered in the interceptors are global interceptor interceptor intercepts all requests
  • As 2 | 6shown, registered under interceptor interceptor is a partial blocker, need to explicitly configure the request interceptor which intercept.
  • As 3shown, block all paths represents
  • As 4shown, under the premise represents intercept certain requests, excluding / not intercept some requests
  • As 7shown, expressed blocks all /hellopaths beginning with

When a plurality of interceptors, calling sequence press preHandlemethod and then reverse calls each interceptor postHandleand afterCompletionmethods, namely:

  • A for
  • B for
  • For C
  • C-post
  • C-after
  • B-post
  • B-after
  • A-post
  • A-after

2, POST requests Chinese garbled

Spring MVC provides a dedicated Filter POST request to solve the garbage problem, you should be configured to use the web.xml file:

<filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

get request, can generally be resolved by modifying the configuration settings file tomcat.

Guess you like

Origin www.cnblogs.com/whitespaces/p/12455031.html