随笔 - 6  文章 - 129  trackbacks - 0
<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

常用链接

留言簿(14)

随笔档案(6)

文章分类(467)

文章档案(423)

相册

收藏夹(18)

JAVA

搜索

  •  

积分与排名

  • 积分 - 821550
  • 排名 - 49

最新评论

阅读排行榜

评论排行榜

在映射请求中添加一个拦截器,自定义拦截器必须实现org.springframework.web.servlet.HandlerInterceptor接口。
HandlerInterceptor的示例代码如下:
public interface HandlerInterceptor
{
   //在处理器执行前调用
boolean preHandle(HttpServletRequest request, HttpServletResponse response,   Object handler) throws Exception;

   //在处理器执行后调用
void postHandle(HttpServletRequest request, HttpServletResponse response,   Object handler,   ModelAndView modelAndView ) throws Exception;
   //在整个请求处理完后调用
void afterCompletion(HttpServletRequest request, HttpServletResponse response,   Object handler,   Exception    ex) throws Exception;
}
----------------------------------------------------------------------------------------------------------------------------
文件NotLeaveWord.java
package dgut.ke.interceptor;

import java.util.Calendar;

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

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

public class NotLeaveWord extends HandlerInterceptorAdapter
{
 private int startTime;
 private int endTime;
 private String interceptorPage;
  
 /*通过依赖注入*/
 public void setStartTime(int startTime) {
  this.startTime = startTime;
 }
 public void setEndTime(int endTime) {
  this.endTime = endTime;
 }
 public void setInterceptorPage(String interceptorPage) {
  this.interceptorPage = interceptorPage;
 }
 public int getStartTime() {
  return startTime;
 }
 public int getEndTime() {
  return endTime;
 }
 public String getInterceptorPage() {
  return interceptorPage;
 }
 /*进行拦截处理*/
 public boolean preHandle(HttpServletRequest request,
       HttpServletResponse response,
       Object handler)throws Exception
 {
  Calendar cal = Calendar.getInstance();
  int hour = cal.get(java.util.Calendar.HOUR_OF_DAY);
  if( hour > startTime && hour < endTime)
  {
   request.setAttribute("startTime", this.getStartTime());
   request.setAttribute("endTime", this.getEndTime());
   RequestDispatcher rd = request.getRequestDispatcher(this.getInterceptorPage());
   rd.forward(request, response);
   return false;
  }
  else
  {
   return true ;
  } 
 }
 
}
-----------------------------------------------------------------------------------------------------------------

文件dispatcherServlet-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
<beans>
<!-- 定义映射 -->
 <bean id="urlMapping"
  class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"
  abstract="false" singleton="true" lazy-init="default"
  autowire="default" dependency-check="default">
  <!-- 设置拦截器 -->
  <property name="interceptors">
   <list>
    <ref local="notLeaveWord"/>
   </list>
  </property>
  <property name="mappings">
   <props>
    <prop key="helloWorld.do">helloWorld</prop>
   </props>
  </property>
 </bean>
<!-- 定义视图 -->
 <bean id="viewResolver"
  class="org.springframework.web.servlet.view.InternalResourceViewResolver"
  abstract="false" singleton="true" lazy-init="default"
  autowire="default" dependency-check="default">
  <property name="viewClass">
   <value>
   org.springframework.web.servlet.view.JstlView
   </value>
  </property>
  <!-- 定义jsp所在的目录 -->
  <property name="prefix"><value>/</value></property>
  <property name="suffix"><value>.jsp</value></property>
 </bean>
<!-- 定义控制器 -->
 <bean id="helloWorld" class="dgut.ke.action.HelloWorld"
  abstract="false" singleton="true" lazy-init="default"
  autowire="default" dependency-check="default">
  <property name="helloWorld">
   <value>Spring MVC 例子</value>
  </property>
  <property name="viewPage">
   <value>index</value>
  </property>
 </bean>
<!-- 拦截器设置 -->
 <bean id="notLeaveWord" class="dgut.ke.interceptor.NotLeaveWord"
  abstract="false" singleton="true" lazy-init="default"
  autowire="default" dependency-check="default">
  <property name="startTime">
   <value>10</value>
  </property>
  <property name="endTime">
   <value>20</value>
  </property>
  <property name="interceptorPage">
   <value>outTime.jsp</value>
  </property>
 </bean>
</beans>
-------------------------------------------------------------------------------------------------------------
文件web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 
  <servlet>
   <servlet-name>dispatcherServlet</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <!-- 初始化参数 -->
   <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/dispatcherServlet-servlet.xml</param-value>
   </init-param>
   <load-on-startup>1</load-on-startup>
  </servlet>
 
  <servlet-mapping>
   <servlet-name>dispatcherServlet</servlet-name>
   <url-pattern>*.do</url-pattern>
  </servlet-mapping>
 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


posted on 2007-08-19 15:46 Ke 阅读(596) 评论(1)  编辑  收藏 所属分类: spring

FeedBack:
# re: 拦截器[未登录] 2008-06-30 23:17 aaa
这个和《spring从入门到精通》里是一样的,我试过了,没有用哦……
在prehander里加个控制台输入可以发现没有用……  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: