场景描述:对特定的请求进行拦截
基于视图定位进行
1.视图控制器IndexController.java
1 package controller; 2 3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.HttpServletResponse; 5 6 import org.springframework.web.servlet.ModelAndView; 7 import org.springframework.web.servlet.mvc.Controller; 8 9 public class IndexController implements Controller { 10 public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { 11 ModelAndView mav = new ModelAndView("index"); 12 mav.addObject("message", "Hello Spring MVC————————视图定位"); 13 return mav; 14 } 15 }
2.拦截器IndexInterceptor.java
1 package interceptor; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Calendar; 5 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 9 import org.springframework.web.servlet.HandlerInterceptor; 10 11 public class IndexInterceptor implements HandlerInterceptor { 12 /** 13 * 在业务处理器处理请求之前被调用 如果返回false 从当前的拦截器往回执行所有拦截器的afterCompletion(),再退出拦截器链 14 * 如果返回true 执行下一个拦截器,直到所有的拦截器都执行完毕 再执行被拦截的Controller 然后进入拦截器链, 15 * 从最后一个拦截器往回执行所有的postHandle() 接着再从最后一个拦截器往回执行所有的afterCompletion() 16 */ 17 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { 18 System.out.println("preHandle(),在访问Controller之前被调用"); 19 return true; 20 } 21 22 /** 23 * 在业务处理器处理请求执行完成后,生成视图之前执行的动作 可在modelAndView中加入数据,比如当前时间 24 */ 25 @Override 26 public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, 27 org.springframework.web.servlet.ModelAndView arg3) throws Exception { 28 // TODO Auto-generated method stub 29 Calendar calendar = Calendar.getInstance(); 30 String dateString = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(calendar.getTime()); 31 System.out.println("postHandle(), 在访问Controller之后,访问视图之前被调用,这里可以注入一个时间到modelAndView中,用于后续视图显示"); 32 arg3.addObject("date", "由拦截器生成的时间:" + dateString); 33 } 34 35 /** 36 * 在DispatcherServlet完全处理完请求后被调用,可用于清理资源等 37 * 38 * 当有拦截器抛出异常时,会从当前拦截器往回执行所有的拦截器的afterCompletion() 39 */ 40 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) 41 throws Exception { 42 System.out.println("afterCompletion(), 在访问视图之后被调用"); 43 } 44 45 }
3.myspringmvc-servlet.xml
修改myspringmvc-servlet.xml以对/index路径进行拦截
如果要拦截其他路径:
/** 拦截所有
/category/** 拦截/category路径下的所有
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context-3.0.xsd 9 http://www.springframework.org/schema/mvc 10 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 11 " 12 > 13 <bean id="viewResolver" 14 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 15 <property name="prefix" value="/WEB-INF/page/" /> 16 <property name="suffix" value=".jsp" /> 17 </bean> 18 19 <bean id="simpleUrlHandlerMapping" 20 class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 21 <property name="mappings"> 22 <props> 23 <prop key="/index">indexController</prop> 24 </props> 25 </property> 26 </bean> 27 <bean id="indexController" class="controller.IndexController"></bean> 28 <mvc:interceptors> 29 <mvc:interceptor> 30 <mvc:mapping path="/index"/> 31 <!-- 定义在mvc:interceptor下面的表示是对特定的请求才进行拦截的 --> 32 <bean class="interceptor.IndexInterceptor"/> 33 </mvc:interceptor> 34 <!-- 当设置多个拦截器时,先按顺序调用preHandle方法,然后逆序调用每个拦截器的postHandle和afterCompletion方法 --> 35 </mvc:interceptors> 36 37 </beans>
4.web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 5 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 6 <servlet> 7 <servlet-name>myspringmvc</servlet-name> 8 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 9 <load-on-startup>1</load-on-startup> 10 </servlet> 11 <servlet-mapping> 12 <servlet-name>myspringmvc</servlet-name> 13 <url-pattern>/</url-pattern> 14 </servlet-mapping> 15 </web-app>
5.index.jsp WEB-INF/page目录下
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'index2.jsp' starting page</title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 23 </head> 24 25 <body> 26 <h1>${message}</h1> 27 <h2>${date }</h2> 28 </body> 29 </html>
6.测试