12.1、拦截器——HelloWorld

来源:互联网 发布:淘宝海外代购dw可信吗 编辑:程序博客网 时间:2024/06/04 18:34

Spring MVC也可以使用拦截器对请求进行拦截处理,用户可以自定义拦截器来实现特定的功能,自定义的拦截器必须实现HandlerInterceptor接口。接口中有以下三个方法:

– preHandle():这个方法在业务处理器处理请求之前被调用,在该方法中对用户请求 request 进行处理。如果程序员决定该拦截器对请求进行拦截处理后还要调用其他的拦截器,或者是业务处理器去进行处理,则返回true;如果程序员决定不需要再调用其他的组件去处理请求,则返回false。
– postHandle():这个方法在业务处理器处理完请求后,但是DispatcherServlet 向客户端返回响应前被调用,在该方法中对用户请求request进行处理。

– afterCompletion():这个方法在 DispatcherServlet 完全处理完请求后被调用,可以在该方法中进行一些资源清理的操作。

下面示例:定义一个拦截器,首先执行一个HelloWorld的请求,观察拦截器中方法执行的顺序。

一、spring配置文件和web配置文件
springmvc.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:aop="http://www.springframework.org/schema/aop"    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/aop http://www.springframework.org/schema/aop/spring-aop-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">    <!--包扫描的范围-->    <context:component-scan base-package="com.lzj.springmvc"></context:component-scan>      <!--定义视图解析器-->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/WEB-INF/views/"></property>        <property name="suffix" value=".jsp"></property>    </bean>    <!--定义请求的拦截器-->    <mvc:interceptors>        <bean class="com.lzj.springmvc.interceptors.FirstInterceptor"></bean>    </mvc:interceptors></beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns.jcp.org/xml/ns/javaee"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"id="WebApp_ID" version="3.1"><!-- The front controller of this Spring Web application, responsible for handling all application requests --><servlet><servlet-name>springDispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><!-- Map all requests to the DispatcherServlet for handling --><servlet-mapping><servlet-name>springDispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>

二、创建拦截器

package com.lzj.springmvc.interceptors;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;public class FirstInterceptor implements HandlerInterceptor{    @Override    public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)            throws Exception {        System.out.println("afterCompletion");    }    @Override    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)            throws Exception {        System.out.println("postHandle");    }    @Override    public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {        System.out.println("preHandle");        return true;    }}

三、创建一个请求
在WebContent下创建一个index.jsp文件,其内容为:

<a href="helloworld">hello world</a>

四、创建控制器
用于截获index.jsp中的helloworld请求

@Controller@RequestMapping("/springMVC")public class TestSpringMVC {    @RequestMapping("/testRequestMapping")    public String testRequestMapping(){        System.out.println("testRequestMapping");        return "success";    }}

当点击index.jsp中的请求连接时,拦截器拦截请求,执行preHandle方法,然后控制器截获请求,执行控制器testRequestMapping方法,当控制器执行结束后并返回时,拦截器拦截请求,执行postHandle方法,最后释放资源,执行afterCompletion。输出结果如下:

preHandlehello worldpostHandleafterCompletion
原创粉丝点击