struts2(四)拦截器
来源:互联网 发布:crocs洞洞鞋淘宝 编辑:程序博客网 时间:2024/05/16 15:21
Struts2拦截器的生命周期
step1:tomcat服务器启动时,创建所有拦截器,并执行所有初始化方法;
step2:用户访问时,先创建Action实例,后调用拦截器的interceptor()方法;
拦截器执行invoke()方法依次调用其他的拦截器,最后调用action的相应方法;返回时,按顺序依次返回到对应拦截器,最后返回到tomcat,然后返回相应页面;
自定义拦截器
当struts.xml中没有指定执行拦截器时,会执行默认的default-struts中的拦截器栈中所有拦截器
自定义的拦截器
- 写一个类实现interceptor接口
- 在struts.xml中配置自定义拦截器
自定义的拦截器类
public class HelloInterceptor implements Interceptor{ // 服务器启动时执行 public HelloInterceptor(){ System.out.println("创建拦截器对象"); } @Override public void init() { // TODO Auto-generated method stub System.out.println("执行拦截器初始化方法"); } @Override public String intercept(ActionInvocation arg0) throws Exception { System.out.println("用户访问:拦截器执行业务方法"); String invoke = arg0.invoke(); System.out.println("返回action执行完回到拦截器"); return invoke; } @Override public void destroy() { // TODO Auto-generated method stub System.out.println("销毁拦截器"); }}
配置的struts.xml
<package name="xxs" extends="struts-default"> <!-- 用户自定义拦截器配置 --> <interceptors> <interceptor name="hellointerceptor" class="com.wsk.a_hello.HelloInterceptor"></interceptor> <!-- 自定义一个栈,加入自定义的拦截器,注意另外还需要引用默认拦截器栈 --> <interceptor-stack name="hellostack"> <!-- 引用默认拦截器栈,一定要放到第一行 --> <interceptor-ref name="defaultStack"></interceptor-ref> <!-- 引用自定义拦截器 --> <interceptor-ref name="hellointerceptor"></interceptor-ref> </interceptor-stack> </interceptors> <!-- 执行拦截器栈 --> <default-interceptor-ref name="hellostack"></default-interceptor-ref> <!-- action配置 --> <action name="hello" class="com.wsk.a_hello.HelloAction" method="hello"> <result name="success">/hello.jsp</result> </action> </package>
执行的action
public class HelloAction extends ActionSupport{ public String hello(){ System.out.println("helloAction!"); return this.SUCCESS; }}
阅读全文
0 0
- struts2(四)拦截器
- Struts2框架(四)拦截器
- struts2 18拦截器详解(四) --- ServletConfigInterceptor
- struts2入门教程四(拦截器)
- Struts2(四)使用Struts2中的拦截器
- Struts2(四)使用Struts2中的拦截器
- 【SSH】Struts2学习(四)拦截器、struts2标签
- Struts2学习笔记(四)——拦截器
- 菜鸟学SSH(四)——Struts2拦截器
- Struts2自学入门(四)——简单拦截器
- javaweb之struts2学习四---自定义拦截器
- struts2学习笔记(四)拦截器与拦截器栈
- 【Struts2四】利用拦截器对登陆进行拦截的小案例
- 【Struts2】Struts2中拦截器
- Struts2最新版(2.5.12)快速入门(四) struts2之拦截器(Interceptor)
- struts2拦截器
- Struts2拦截器
- struts2.0拦截器
- JS原型
- 2017年上海金马五校程序设计竞赛:Problem E : Find Palindrome
- Special Judge Ⅰ
- domReady
- 高德地图VueAPI(vue-amap)学习记录
- struts2(四)拦截器
- 给 Android 开发者的 RxJava 详解
- 获取class
- 28岁学习PHP,并不晚!
- spring批量更新数据 ---- BatchPreparedStatementSetter
- 颜色转换
- 站点信息配置-使用数据库
- 编译原理:求产生式的FIRST集合
- Special Judge Ⅱ