SpringMVC拦截器详解
程序员文章站
2022-06-25 10:18:12
目录springmvc配置文件:web.xml文件
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"> <!-- 配置springmvc*处理器--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <!-- 初始化的时候配置加载springmvc的配置文件--> <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> <!-- 3、修改web.xml中的请求路径地址方式--> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- 3、修改web.xml中的请求路径地址方式--> <url-pattern>/</url-pattern> <!-- <url-pattern>*.do</url-pattern>--> </servlet-mapping> <!-- 1、将css文件使用默认处理方式进行处理,不再交由*控制器处理, 设置路径的更高匹配优先级 --> <!-- <servlet-mapping>--> <!-- <servlet-name>default</servlet-name>--> <!-- <url-pattern>*.css</url-pattern>--> <!-- <url-pattern>*.js</url-pattern>--> <!-- <url-pattern>*.jpg</url-pattern>--> <!-- <url-pattern>*.png</url-pattern>--> <!-- <url-pattern>*.html</url-pattern>--> <!-- </servlet-mapping>--> </web-app>
springmvc配置文件:
<?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 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--自动扫描包--> <!-- 开启ioc 注解事务支持--> <context:component-scan base-package="cn"></context:component-scan> <!--开启spiring mvc注解支持--> <mvc:annotation-driven></mvc:annotation-driven> <!--配置spring 中的视图解析器--> <bean class="org.springframework.web.servlet.view.internalresourceviewresolver" id="resolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> <mvc:interceptors> <bean id="interceptor1" class="cn.hp.interceptor.testinterceptor1"></bean> <mvc:interceptor> <mvc:mapping path="/**"/> <bean id="interceptor1" class="cn.hp.interceptor.testinterceptor1"></bean> </mvc:interceptor> <mvc:interceptor> <mvc:mapping path="/test1.do"/> <mvc:exclude-mapping path="/test2.do"/> <bean id="interceptor2" class="cn.hp.interceptor.testinterceptor2"></bean> </mvc:interceptor> </beans>
controller类:
package cn.hp.action; import org.springframework.stereotype.controller; import org.springframework.ui.model; import org.springframework.web.bind.annotation.requestmapping; import javax.servlet.http.httpsession; @controller public class useraction { @requestmapping("/test1.do") public string test01(){ system.out.println("正在执行test1这个业务逻辑"); return "index"; } @requestmapping("/test2.do") public string test02(){ system.out.println("正在执行test2这个业务逻辑"); return "index"; } }
第一个拦截器配置:
package cn.hp.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 testinterceptor1 implements handlerinterceptor { @override public boolean prehandle(httpservletrequest request, httpservletresponse response, object handler) throws exception { system.out.println("第一个拦截器,执行业务逻辑之前执行此方法"); return true; } @override public void posthandle(httpservletrequest request, httpservletresponse response, object handler, modelandview modelandview) throws exception { system.out.println("第一个拦截器,执行业务逻辑之后,视图渲染之前执行此方法"); } @override public void aftercompletion(httpservletrequest request, httpservletresponse response, object handler, exception ex) throws exception { system.out.println("第一个拦截器,执行视图渲染之后执行此方法"); } }
第二个拦截器:
package cn.hp.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 testinterceptor2 implements handlerinterceptor { @override public boolean prehandle(httpservletrequest request, httpservletresponse response, object handler) throws exception { system.out.println("第二个拦截器,执行业务逻辑之前执行此方法"); return true; } @override public void posthandle(httpservletrequest request, httpservletresponse response, object handler, modelandview modelandview) throws exception { system.out.println("第二个拦截器,执行业务逻辑之后,视图渲染之前执行此方法"); } @override public void aftercompletion(httpservletrequest request, httpservletresponse response, object handler, exception ex) throws exception { system.out.println("第二个拦截器,执行视图渲染之后执行此方法"); } }
总结
本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注的更多内容!
上一篇: 鸡爪要煮多久才可以去腌制?