拦截器是struts2核心,众多功能都是通过拦截器来实现的
一.自己写一个interceptor
在interceptor方法里写拦截的内容
二.在struts2.xml中配置拦截器
先写<interceptor></interceptor>拦截器定义,然后在action的result后面添加<interceptor-ref>引用
注意:
struts2里的拦截器和servlet的过滤器差不多,不过struts2的拦截器只能加在action前面,因为都配置在action的result前面
简单的话 是自己的拦截器继承AbstactInterceptor
三.方法拦截器(对指定方法拦截)MethdFilterInterceptor
上面拦截器拦截的是action中execute方法,那么自己写的方法怎么拦截
1.写一个interceptor,然后继承MethodFilterInterceptor类
2.在struts.xml中配置给方法拦截器指定拦截的方法:
四.用拦截器验证用户登录
1).写一个登陆的拦截器 在除了login方法外都要对用户是否登陆加以判断
2).定义了这个拦截器那么应该用在除了登陆以外的每个action方法之前
上面方法return Action.Login那么在struts.xml里需要配置一个gloabal-results 名字为login
定义一个interceptor-stack
五.executeAndWaitIntercepto r
当Http请求耗时很长的情况下,用这个拦截器(一方面让用户看到等待页面,另一方面自动刷新请求);
配置如下
1).在struts.xml 配置execAndWait拦截器
2).着手写wait.jsp
六.上面的拦截器Interceptor只能拦截action而拦截不到jsp页面,所以需要servlet的过滤器Filter
1).定义一个自己的Filter
2).在web.xml中配置
——进行验证,等等操作
转载自:http://blog.sina.com.cn/s/blog_aaa756740101hudd.html
一.自己写一个interceptor
![Struts2拦截器深度解读 Struts2拦截器深度解读](https://i-blog.csdnimg.cn/blog_migrate/f689315948f7f7710157c864bf9eed8f.jpeg)
![Struts2拦截器深度解读 Struts2拦截器深度解读](https://i-blog.csdnimg.cn/blog_migrate/1ad85b3c00a801be2e45611542d22303.jpeg)
二.在struts2.xml中配置拦截器
![Struts2拦截器深度解读 Struts2拦截器深度解读](https://i-blog.csdnimg.cn/blog_migrate/b6eb4f2f0e331c558510392a9c1fc195.jpeg)
注意:
三.方法拦截器(对指定方法拦截)MethdFilterInterceptor
![Struts2拦截器深度解读](https://i-blog.csdnimg.cn/blog_migrate/1cede978aad6c7af8faf25c57dcbb1ee.jpeg)
四.用拦截器验证用户登录
![Struts2拦截器深度解读](https://i-blog.csdnimg.cn/blog_migrate/81658e502d06b7a419be409108723a3d.jpeg)
![Struts2拦截器深度解读 Struts2拦截器深度解读](https://i-blog.csdnimg.cn/blog_migrate/4f191a76114a6e31cac4f496cb768b50.jpeg)
![Struts2拦截器深度解读 Struts2拦截器深度解读](https://i-blog.csdnimg.cn/blog_migrate/95e41ee51ff11db6c9fbc76715ce0f0a.jpeg)
![Struts2拦截器深度解读 Struts2拦截器深度解读](https://i-blog.csdnimg.cn/blog_migrate/15f3ee51191b58857316283912c8636d.jpeg)
![Struts2拦截器深度解读 Struts2拦截器深度解读](https://i-blog.csdnimg.cn/blog_migrate/d6c6f015a549ad1885dc51f2f9316a76.jpeg)
![Struts2拦截器深度解读 Struts2拦截器深度解读](https://i-blog.csdnimg.cn/blog_migrate/3dd8acf12164d65214542a11d4d9b2ba.jpeg)
![Struts2拦截器深度解读 Struts2拦截器深度解读](https://i-blog.csdnimg.cn/blog_migrate/a0ba76ab8a6b868d8d9d281599fa9e93.jpeg)
![Struts2拦截器深度解读](https://i-blog.csdnimg.cn/blog_migrate/e3b0b10dfc097f2f8b54908a21ade67e.jpeg)
转载自:http://blog.sina.com.cn/s/blog_aaa756740101hudd.html