springmvc拦截器

7 篇文章 0 订阅
本文详细介绍了如何在Spring MVC中配置全局拦截器,包括创建UserAction业务逻辑、实现HandlerInterceptor接口,以及在web.xml、applicationContext.xml和spring-mvc.xml中的具体配置。通过拦截器实现对/test01的特殊处理和/test02的放行,展示了灵活的URL映射和拦截策略。
摘要由CSDN通过智能技术生成
  • .拦截器主要时配置spring-mvc.xml和配置其他相关的xml文件
  • 首先做业务逻辑,再做拦截器实现HandlerInterceptor接口(为servlet接口)
  • 配置web.xml、applicationContext.xml、spring-mvc.xml

1-1.全局拦截器

1.1创建UserAction 业务逻辑

package com.xiao.action;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class UserAction {
    @RequestMapping("/test01")
    public String test01(){
        System.out.println("正在执行test01的业务逻辑");
        return "index";
    }


}

1.2.做做拦截器实现HandlerInterceptor接口(为servlet接口)

package com.xiao.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 TestInterceptor01 implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        System.out.println("拦截请求之后,执行业务逻辑之前");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        System.out.println("执行业务逻辑之后 视图渲染之前执行");
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        System.out.println("视图渲染之后执行");
    }
}

1.3-1 配置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">

    <!-- 设置web应用的上下文参数 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!--使用spring提供的监听器加载上下文的配置文件 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--no.1 配置spring mvc 的 前端控制器,拦截所有的 请求 -->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

1.3-2.配置applicationContext.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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.0.xsd">

            <!--这里面写  spring的配置文件,主要配置和业务逻辑相关联的-->
             <!-- 数据源 , 事务管理控制,等等和业务逻辑相关联的-->
       <mvc:default-servlet-handler/>

    <mvc:annotation-driven/>
    <context:annotation-config/>
    <!--扫描注解-->
    <context:component-scan base-package="com.xiao"></context:component-scan>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>  <!--如果这个value="/WEB-INF/jsp/" jsp页面要再jsp文件夹下,要不然会找不到前端页面 -->
        <property name="suffix" value=".jsp"></property>
    </bean>

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="utf-8"></property>
        <property name="maxUploadSize" value="102400000"></property>
    </bean>



</beans>

1.3-3.配置spring-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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	<!--配置springmvc的文件,包含 网站跳转!的逻辑相关的控制和配置 -->
	<!--注解扫描 -->

	<context:component-scan base-package="com.xiao" />
	<mvc:default-servlet-handler/>
	<mvc:annotation-driven />
	<!--配置视图解析器 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

	<!--全局拦截器-->
<mvc:interceptors>-->
		<bean id="TestInterceptor01" class="com.xiao.interceptor.TestInterceptor01"></bean>
</mvc:interceptors>


</beans>

1.4.全局拦截器的运行效果

 


2.拦截test01,放行test02

2.1 补充test02业务逻辑和拦截器2

// 业务逻辑
package com.xiao.action;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class UserAction {
    @RequestMapping("/test01")
    public String test01(){
        System.out.println("正在执行test01的业务逻辑");
        return "index";
    }

    @RequestMapping("/test02")
    public String test02(){
        System.out.println("正在执行test02的业务逻辑");
        return "index";
    }
}

--------------------------------------------------------------------------------

// 第二个拦截器
package com.xiao.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 TestInterceptor02 implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        System.out.println("第二个拦截器,拦截请求之后,执行业务逻辑之前");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        System.out.println("第二个拦截器,执行业务逻辑之后 视图渲染之前执行");
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        System.out.println("第二个拦截器,视图渲染之后执行");
    }
}

 2.2 配置spring-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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	<!--配置springmvc的文件,包含 网站跳转!的逻辑相关的控制和配置 -->
	<!--注解扫描 -->

	<context:component-scan base-package="com.xiao" />
	<mvc:default-servlet-handler/>
	<mvc:annotation-driven />
	<!--配置视图解析器 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

	<mvc:interceptors>
		<mvc:interceptor>
			<!--拦截test01,放行test02-->
			<mvc:mapping path="/test01"/>
			<mvc:exclude-mapping path="/test02"/>
			<bean id="testInterceptor02" class="com.xiao.interceptor.TestInterceptor02"></bean>
		</mvc:interceptor>
	</mvc:interceptors>
</beans>

2.3 运行效果

在网址输入test02(不会拦截),效果如下:

 在网址输入test01(会拦截),效果如下:

 


3.1 如果spring-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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	<!--配置springmvc的文件,包含 网站跳转!的逻辑相关的控制和配置 -->
	<!--注解扫描 -->

	<context:component-scan base-package="com.xiao" />
	<mvc:default-servlet-handler/>
	<mvc:annotation-driven />
	<!--配置视图解析器 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

	<mvc:interceptors>
		<!--拦截所有请求-->
		<mvc:interceptor>
			<mvc:mapping path="/**"/>
			<bean id="testInterceptor01" class="com.xiao.interceptor.TestInterceptor01"></bean>
		</mvc:interceptor>

		<mvc:interceptor>
			<!--拦截test01,放行test02-->
			<mvc:mapping path="/test01"/>
			<mvc:exclude-mapping path="/test02"/>
			<bean id="testInterceptor02" class="com.xiao.interceptor.TestInterceptor02"></bean>
		</mvc:interceptor>
	</mvc:interceptors>
</beans>

 在网址输入test01(因为第一个拦截所有请求,代码自上而下执行),运行效果如下:

 在网址输入test02(拦截test01,因为第二个拦截器时拦截test01,放行test02),效果如下:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值