Spring3 MVC的最佳实践和理解(3)

个人学习参考所用,勿喷! 

3.使用拦截器

Spring的拦截器能够在Spring MVC处理程序请求的前后对请求和处理结果进行处理等相关要求得到满足。每个处理拦截其都必须实现HandlerInterceptor接口,这个接口包含三个方法:preHandle()、postHandle()、afterCompletion()分别在处理请求前后和所有请求处理完成后调用。

3.1)实现一个计算并显示每次请求前后时间的自定义拦截器,这里继承HandlerInterceptorAdapter,HandlerInterceptorAdapter对HandlerInterceptor有默认实现:

public class MeasurementInterceptor extends HandlerInterceptorAdapter {

    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
        long startTime = System.currentTimeMillis();
        request.setAttribute("startTime", startTime);
        return true;
    }

    public void postHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler,
			   //Model model) throws Exception {
			   ModelAndView modelAndView) throws Exception {
        long startTime = (Long) request.getAttribute("startTime");
        request.removeAttribute("startTime");
        long endTime = System.currentTimeMillis();
	//model.addAttribute("handlingTime", endTime - startTime);
        modelAndView.addObject("handlingTime", endTime - startTime);
    }
}

3.2)注册上述的拦截器到DefaultAnnotationHandlerMapping这个将拦截器应用到所有控制器的Bean中,这里可以指定多个拦截器。将以下的内容添加到court-servlet.xml根节点下:

<!-- Interceptors -->
<bean id="measurementInterceptor" class="com.apress.springrecipes.court.web.MeasurementInterceptor" />
<!-- Annotation handlers (Applied by default to ALL @controllers -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
	<!-- Interceptors are applied to all annotated controllers -->
	<property name="interceptors">
		<list>
			<ref bean="measurementInterceptor" />
		</list>
	</property>
</bean>

3.3)现在可以在welcome.jsp中取得这个时间:

Handling time : ${handlingTime} ms

3.4)如果想对不同的URL使用不同的拦截器,那必须使用Scott Murply的spring-plugins.jar的辅助了(在http://code.google.com/p/springplugins/downloads/list下载)。这里需要使用个该包下的SelectedAnnotationHandlerMapping。这里还采用了order属性来定义上述注册拦截器的优先级(order值越低,其优先级越高)。

<!-- Interceptors -->
<bean id="measurementInterceptor" class="com.apress.springrecipes.court.web.MeasurementInterceptor" />
<bean id="summaryReportInterceptor" class="com.apress.springrecipes.court.web.ExtensionInterceptor" />

<!-- Annotation handlers (Applied by default to ALL @controllers -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
	<property name="order" value="1" />
	<!-- Interceptors are applied to all annotated controllers -->
	<property name="interceptors">
		<list>
			<ref bean="measurementInterceptor" />
		</list>
	</property>
</bean>	

<!-- Annotation handlers the Bind controller to all URLs under "/reservationSummary*"   -->
<bean id="publicMapper" class="org.springplugins.web.SelectedAnnotationHandlerMapping">
	<property name="order" value="0" />
	<property name="urls">
		<list>
			<value>/reservationSummary*</value>
		</list>
	</property>
	<property name="interceptors">
		<list>
			<ref bean="summaryReportInterceptor" />
		</list>
	</property>
</bean>


参考:

juyon的blog:spring3 MVC国际化支持之中文乱码

Gary Mark等的书籍:《Spring Recipes》2ed

猜你喜欢

转载自kingxss.iteye.com/blog/1496972
今日推荐