在DefaultAnnotationHandlerMapping中使用拦截器

sevlet.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:p="http://www.springframework.org/schema/p"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	
	<!-- 该语句可以使AOP和Spring MVC生效 -->
	<aop:aspectj-autoproxy />
	<!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
	<context:component-scan base-package="com.abrahu.*" />

	
	<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<!-- messageConverters属性的配置可以解决JSON返回中文为问号的问题 -->
		<property name="messageConverters">  
         <list>  
             <bean class = "org.springframework.http.converter.StringHttpMessageConverter">  
                <property name = "supportedMediaTypes">  
                     <list>  
                         <value>text/plain;charset=UTF-8</value>  
                     </list>  
                </property>  
             </bean>  
         </list>  
    </property>  
	</bean>
	
	<!-- 注解依赖的适配器 DefaultAnnotationHandlerMapping -->
	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
		<property name="interceptors">     
	        <list>     
	            <ref bean="myHandlerInterceptorAdapter"/>     
	        </list>     
    	</property> 
	</bean>
	<!-- 可拦截用户请求 -->
	<bean id="myHandlerInterceptorAdapter" class="com.abrahu.util.interceptor.MyHandlerInterceptorAdapter" />
	
	<!-- Spring @AutoWired 依赖自动注入,不需要setter方法 -->
	<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />




	<!-- freemarkerConfig -->
	
	
	<!-- 设置freeMarker的配置文件路径 -->
	
	<bean id="freemarkerConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
		<property name="location" value="classpath:conf/freemarker.properties"/>
	</bean>
	<!-- 不知道这个是做什么用的 -->
	<bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape"/>
	
	<!-- 配置freeMarker的模板路径 -->
	<bean id="freemarkerConfig"
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
		<property name="freemarkerSettings" ref="freemarkerConfiguration" />
		<!-- 
		<property name="freemarkerSettings">
			<props>
				<prop key="auto_import">common/spring.ftl as spring</prop>
			</props>
		</property>
		 -->
		<property name="templateLoaderPath" value="/WEB-INF/ftl/" />
		<property name="defaultEncoding" value="UTF-8" />
		<property name="freemarkerVariables">         
			<map>             
				<entry key="xml_escape" value-ref="fmXmlEscape" />         
			</map>     
		</property>
	</bean>
	
	
	<!-- 配置freeMarker视图解析器 -->
	<bean
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
		<property name="cache" value="true" />
		<property name="prefix" value="" />
		<property name="suffix" value=".ftl" />
		<property name="exposeSpringMacroHelpers" value="true" />
		<property name="exposeRequestAttributes" value="true" />
		<property name="exposeSessionAttributes" value="true" />
		<property name="requestContextAttribute" value="request" />
		<property name="contentType" value="text/html; charset=utf-8" />
	</bean>


</beans>


MyHandlerInterceptorAdapter.java

 

package com.abrahu.util.interceptor;

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

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class MyHandlerInterceptorAdapter extends HandlerInterceptorAdapter {
	//参考网址:http://lijackly.iteye.com/blog/805455
	/**
	 * bean处理前,可以进行编码、安全控制等处理.
	 */
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
	    throws Exception {
		return true;
	}

	/**
	 * bean处理后,有机会修改ModelAndView.
	 */
	public void postHandle(
			HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
			throws Exception {
	}

	/**
	 * 可以根据ex是否为null判断是否发生了异常,进行日志记录.
	 */
	public void afterCompletion(
			HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}
}


 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值