Struts拦截器
来源:互联网 发布:淘宝逆战号出售平台 编辑:程序博客网 时间:2024/05/29 13:43
Struts拦截器
struts.xml配置文件中注册:
<interceptorname="interceptorName"class="interceptorClass"/>
或者
<interceptorname="interceptorName"class="interceprotClass">
<paramname="paramName">paramValue</param>
</interceptor>
拦截器栈:
<interceptors>
<interceptorname="interceptor1"class="interceptorClass"/>
<interceptorname="interceptor2"class="interceprotClass"/>
<interceptor-stackname="myStack">
<interceptor-refname="interceptor1"/>
<interceptor-refname="interceptor2"/>
</interceptor-stack>
</interceptors>
配置默认拦截器(每个包下只能定义一个默认拦截器):
<default-interceptor-refname="myStack"/>
只要自定义包继承了struts-default默认包,就会继承其内置的各个拦截器和其默认的拦截器栈。struts-dafault包的默认拦截器栈会作用于所有的这些Action。
使用拦截器时,需要在Action中进行配置,通过<interceptor-ref>标签类指定在Action中使用的拦截器。
<actionname="login"class="com.chen.action.LoginAction">
<resultname="input">/login.jsp</result>
<interceptor-refname="interceptor1"/>
<interceptor-refname="interceptor2">
<paramname="paramName">paramValue</param>
</interceptor-ref>
</action>
Struts2的默认拦截器
alias拦截器
该拦截器的作用是给参数起一个别名,实现Action之间的传值。
在struts.xml文件中的<action>标签下,添加以下标签:
<param name="aliases">#{'name1':'value1','name2':'value2'}</param>
在调用包含上面代码的Action时,Struts2框架会将Action莲中当前Action的前一个Action的name1和name2属性取出,赋给当前Action的value1和value2属性,这样就完成了Action之间的传值。
注释:从一个Action调到另一个Action,可以通过设置result元素的type属性来实现。有两种方法:第一种是将type设置为chain结果类型;第二种是将types属性设置为redirectAction类型。
prepare拦截器
该拦截器用来调用实现了Preparable接口的Action的prepare()方法。
如果一个Action实现了Preparable接口,则prepare拦截器会在执行execute()方法之前先执行prepare()方法。
由于Action所在的包基本上都继承了struts-default包,因此也会继承其默认拦截器栈defaultStack,而在defaultStack中配置了prepare拦截器,所以prepare拦截器不需要在struts.xml文件中配置,就会被正确的调用。
timer拦截器
timer拦截器的功能是输出调用Action所需要的时间,它记录了execute方法和在timer后面定义的其他拦截器的intercept方法的执行时间之和,其时间单位为毫秒。
<actionname="login"class="com.chen.action.LoginAction">
<interceptor-refname="timer"/>
<resultname="input">/login.jsp</result>
</action>
自定义拦截器
Struts2拦截器在访问某个Action 方法之前或之后实施拦截。
Struts2 拦截器是可插拔的, 拦截器是 AOP 的一种实现。
编写自定义拦截器
自定义拦截器有两种定义方法,一种是要实现com.opensymphony.xwork2.interceptor.Interceptor接口;另一种是继承AbstractInterceptor类来定义拦截器。
(1)实现Interceptor接口定义拦截器:
publicclass PermissionInterceptorimplements Interceptor {
privatefinalstaticlongserialVersionUID=1L;
@Override
publicvoid destroy() {
}
@Override
publicvoid init() {
}
@Override
public String intercept(ActionInvocationinvocation) throws Exception {
String result=null;
//提取session[username]
Object username=ActionContext.getContext().getSession().get("username");
//验证是否登录(若登录会存在session)
if(username!=null&&!username.equals("")){
result=invocation.invoke();
}else{
result="login";
}
System.out.println(result);
returnresult;
}
}
(2)继承AbstractInterceptor类定义拦截器
publicclassMyInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocationinvocation) throws Exception {
//调用下一个拦截器
String result=invocation.invoke();
returnresult;
}
}
配置自定义拦截器
在struts.xml文件中的<package>标签下:
<interceptors>
<interceptorname="mylogin"class="com.chen.interceptor.PermissionInterceptor"></interceptor>
</interceptors>
方法过滤拦截器
继承MethodFilterInterceptor类的子类能够有选择的拦截Action中的某个指定方法。
继承MethodFilterInterceptor类的子类必须实现doIntercept()方法,用来实现真正的拦截逻辑。
编写方法过滤拦截器:
publicclassMyMethodFilterInterceptor extends MethodFilterInterceptor {
@Override
protected String doIntercept(ActionInvocationinvocation) throws Exception {
String result=invocation.invoke();
returnresult;
}
}
在struts.xml中配置方法过滤拦截器
<interceptors>
<interceptorname="FilterInterceptor"class="com.chen.interceptor.MyMethodFilterInterceptor"/>
</interceptors>
<actionname="filterAction"class="com.chen.action.FilterAction">
<resultname="success">/success.jsp</result>
<interceptor-refname="FilterInterceptor">
<!--指定拦截方法 -->
<paramname="includeMethods">execute,method1</param>
<!--指定不拦截方法 -->
<paramname="excludeMethods">method2</param>
</interceptor-ref>
</action>
- Struts 2.0 拦截器
- struts拦截器
- struts 拦截器
- Struts 2拦截器
- Struts拦截器
- Struts 2拦截器
- struts拦截器
- Struts之拦截器
- struts拦截器
- Struts 拦截器
- struts拦截器
- struts 2 拦截器
- struts 拦截器
- struts自定义拦截器
- struts 拦截器 interceptor
- struts 拦截器总结
- struts拦截器使用方法
- struts 拦截器
- 科锐课堂笔记:2017/2/28 循环相关
- 3ActiveMQ的使用方法
- Eclipse中的快捷键
- Java虚拟机学习
- android--unable to access android sdk
- Struts拦截器
- 数据库系统的三级模式结构
- ActiveMQ整合Spring
- [架构] 架构师应具备的能力
- Java虚拟机学习
- Java ExecutorService四种线程池的例子与说明
- 跳槽,工作,另谋出路?
- javascrpt 函数
- 利用js的闭包原理做对象封装及调用