spring mvc 拦截器


先配置spring mvc的前段控制器

<servlet>
	    <servlet-name>spring-mvc</servlet-name>
	    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	    <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml -->
	    <init-param>
	        <param-name>contextConfigLocation</param-name>
	        <param-value>classpath:/spring-config/spring-mvc.xml</param-value>
	    </init-param>
	    <load-on-startup>2</load-on-startup>
	</servlet>
		 <servlet-mapping>
        <servlet-name>spring-mvc</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
	<servlet-mapping>
	    <servlet-name>spring-mvc</servlet-name>
	    <url-pattern>*.do</url-pattern>
	</servlet-mapping>

在配置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:context ="http://www.springframework.org/schema/context" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
		http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.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"
        default-lazy-init="true" >
	<description>Spring Configuration</description>
	<!-- 引入属性文件 -->
	<context:property-placeholder location="classpath:/jdbc.properties" />
	 
    <!--使Spring支持自动检测组件,如注解的Controller-->
    <context:component-scan base-package="com.zjxx.jp"/>
    <mvc:annotation-driven/> 
    <mvc:resources mapping="/**" location="/" /> 
    <aop:aspectj-autoproxy proxy-target-class="true"/>   
    
    <!-- 上传文件 -->
		<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
				<property name="maxUploadSize" value="500000000000"></property>
		</bean> 
   <!-- 视图文件解析配置 -->
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="${web.pub.prefix}"/>
		<property name="suffix" value="${web.pub.suffix}"/>
   </bean>
   
     <aop:config>
		   <aop:aspect id="MyAspect" ref="myaspect">
		   <aop:pointcut id="logPointCut" expression="execution(* com.zjxx.jp.controller.ZjxxInformationController.createContentHtml(..))"/>
		   <aop:around method="performanceAround" pointcut-ref="logPointCut"/>
		   </aop:aspect>
   </aop:config> 
   
   <!-- 拦截器 -->
	<mvc:interceptors>
	    <!-- 多个拦截器,顺序执行 -->
	    <mvc:interceptor>
	       <mvc:mapping path="/**" /> <!-- 如果不配置或/*,将拦截所有的Controller -->
	       <bean class="com.wy.interceptor.CommonInterceptor"></bean>
	    </mvc:interceptor>
	</mvc:interceptors>
</beans>


在写实现拦截器的类:

public class CommonInterceptor  implements HandlerInterceptor{//实现这个接口
	
	@Autowired
	SqlSessionTemplate sqlSessionTemplate;

	@Override//拦截刚进来的时执行的
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		// TODO Auto-generated method stub
		String requestURI = request.getRequestURI();
		if (requestURI.contains(".do")) {
			return true;
		}
		else if (requestURI.contains("pub/admin/")&&requestURI.contains(".html")) {
			return true;
		}else{
			
		}
		
	}

	@Override//执行controller之后执行的
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
	}

	@Override//完成所有的之后执行的
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		
	}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值