码迷,mamicode.com
首页 > 其他好文 > 详细

Filter-Interceptor

时间:2020-06-22 12:41:43      阅读:50      评论:0      收藏:0      [点我收藏+]

标签:控制   javaee   over   after   style   list   ide   ppi   context   

基于springMVC

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <servlet>
        <servlet-name>HelloMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.form</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>firstTestFilter</filter-name>
        <filter-class>controller.filter.FirstTestFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>firstTestFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter>
        <filter-name>secondTestFilter</filter-name>
        <filter-class>controller.filter.secondTestFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>secondTestFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


</web-app>

mvc的xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--    控制器类所在的包-->
    <context:component-scan base-package="controller"/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--    /WEB-INF/page/意为页面的路径,.jsp为页面格式-->
        <property name="prefix" value="/WEB-INF/page/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <mvc:interceptors>
        <mvc:interceptor>
            <!-- 对所有请求都拦截,公共拦截器 -->
            <mvc:mapping path="/**"/>
            <bean name="testInterceptor"  class="controller.interceptor.TestInterceptor" />
        </mvc:interceptor>
    </mvc:interceptors>
</beans>

过滤器

FirstTestFilter

package controller.filter;

import javax.servlet.*;
import java.io.IOException;

public class FirstTestFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("FirstTestFilter.init()");
    }

    public FirstTestFilter() {
        System.out.println("FirstTestFilter.FirstTestFilter()构造方法");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("FirstTestFilter.doFilter()进来了");
        filterChain.doFilter(servletRequest,servletResponse);
        System.out.println("FirstTestFilter.doFilter()出去了");
    }

    @Override
    public void destroy() {

    }
}

  secondTestFilter

package controller.filter;

import javax.servlet.*;
import java.io.IOException;

public class secondTestFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("secondTestFilter.init()");
    }

    public secondTestFilter() {
        System.out.println("secondTestFilter.secondTestFilter()");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("secondTestFilter.doFilter()进来了");
        filterChain.doFilter(servletRequest, servletResponse);
        System.out.println("secondTestFilter.doFilter()出去了");
    }

    @Override
    public void destroy() {

    }
}

  两个过滤在web.xml中配置,按顺序依次执行,先进先出原则

 

拦截器

TestInterceptor

package controller.interceptor;


import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestInterceptor implements HandlerInterceptor {


    public TestInterceptor() {
        System.out.println("TestInterceptor的构造方法");
    }


    //访问action方法之前
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("TestInterceptor进入拦截器");
        return true;
    }

    //访问action之后,返回到页面之前
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        request.setAttribute("hh","ss");
    }

    //action结束之后,用于释放资源
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        request.setAttribute("hh","aa");
    }
}

  先执行过滤器后执行拦截器,主要用于对访问action之前和之后的拦截,在mvc的配置文件中配置

Filter-Interceptor

标签:控制   javaee   over   after   style   list   ide   ppi   context   

原文地址:https://www.cnblogs.com/Fan-Mister/p/13175936.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!