SPRINGMVC Interceptor and Filter Summary

Source: Internet
Author: User

Interceptor: Used to intercept the URL of the access

Use: Permission validation, garbled settings, etc.

Configuration in the Spring-mvc.xml file:

<Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"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/sprin G-beans-3.1.xsd Http://www.springframework.org/schema/context HTTP://WWW.SPR Ingframework.org/schema/context/spring-context-3.1.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/MV C  http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframe Work.org/schema/tx "                       >

    <!--Writing interceptors -    <mvc:interceptors>        <!--interception of specific URLs -        <Mvc:interceptor>            <mvc:mappingPath= "/test.do"/>            <Beanclass= "Com.hbut.interceptor.TestInterceptor"/>        </Mvc:interceptor>        <Mvc:interceptor>            <!--interception of specific modules -            <mvc:mappingPath= "/test"/>            <Beanclass= "Com.hbut.interceptor.TestInterceptor"/>        </Mvc:interceptor>    </mvc:interceptors>

Interception of all URLs

  < mvc:interceptors >      <  class= "Org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>  </mvc:interceptors>

Java code

 PackageCom.hbut.interceptor;ImportOrg.springframework.web.servlet.HandlerInterceptor;ImportOrg.springframework.web.servlet.ModelAndView;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;/*** @Author Xijun.gong * @DATE 2016/6/1. * Aim:com.hbut.interceptor*/ Public classTestinterceptorImplementsHandlerinterceptor {@Override Public BooleanPrehandle (HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse, Object o)throwsException {
Todo add here to manipulate the code
System.out.println ( "Prehandle"); return true; When Todo here is false, the request does not reach the control layer} @Override Public voidPosthandle (HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse, Object o, ModelAndView Modelandview)throwsException {System.out.println ("Posthandle"); Todo can be used to modify information, jump, etc.} @Override Public voidAftercompletion (HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse, Object o, Exception E )throwsException {System.out.println ("Aftercompletion"); Todo Last execution}}

Another type of interceptor: Similar

 PackageCom.hbut.interceptor;ImportOrg.springframework.ui.ModelMap;Importorg.springframework.web.context.request.WebRequest;ImportOrg.springframework.web.context.request.WebRequestInterceptor;/*** @Author Xijun.gong * @DATE 2016/6/1. * Aim:com.hbut.interceptor*/ Public classTest2interceptorImplementsWebrequestinterceptor {@Override Public voidPrehandle (WebRequest WebRequest)throwsException {} @Override Public voidPosthandle (WebRequest WebRequest, Modelmap modelmap)throwsException {} @Override Public voidAftercompletion (WebRequest WebRequest, Exception e)throwsException {}}

Filter: Dependent on servlet container, using callback function, filtering range is large

Interceptors: Rely on framework containers such as spring, mybatis, flexible

Reference article: 1. Http://blog.sina.com.cn/s/blog_6a0cd5e501012bt9.html

2. http://haohaoxuexi.iteye.com/blog/1750680

SPRINGMVC Interceptor and Filter Summary

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.