Chinaunix首页 | 论坛 | 博客
  • 博客访问: 439446
  • 博文数量: 101
  • 博客积分: 578
  • 博客等级: 中士
  • 技术积分: 872
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-12 22:24
文章分类

全部博文(101)

文章存档

2015年(1)

2014年(83)

2012年(17)

我的朋友

分类: Java

2014-03-13 16:26:49

自己写的拦截器继承
HandlerInterceptorAdapter
这个类是实现了HandlerInterceptor接口的有三个方法preHandle(..)postHandle(..)
执行时afterCompletion(..)


如下例子::::【定义自己的类】

 

1. package samples;  

2.    

3. public class TimeBasedAccessInterceptor extends HandlerInterceptorAdapter {  

4.    

5.      private int openingTime;  

6.      private int closingTime;  

7.    

8.      public void setOpeningTime(int openingTime) {  

9.          this.openingTime = openingTime;  

10.     }  

11.   

12.     public void setClosingTime(int closingTime) {  

13.         this.closingTime = closingTime;  

14.     }  

15.   

16.     public boolean preHandle(  

17.             HttpServletRequest request,  

18.             HttpServletResponse response,  

19.             Object handler) throws Exception {  

20.   

21.         Calendar cal = Calendar.getInstance();  

22.         int hour = cal.get(HOUR_OF_DAY);  

23.         if (openingTime <= hour && hour < closingTime) {  

24.             return true;  

25.         } else {  

26.             response.sendRedirect("");  

27.             return false;  

28.         }  

29.     }  

30. }  

 

配置xml bean:

1.   

2.      

3.            class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">  

4.            

5.                

6.                    

7.                

8.            

9.        

10.   

11.     

12.           class="samples.TimeBasedAccessInterceptor">  

13.           

14.           

15.       

16.  

阅读(2879) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~