Struts2拦截器
来源:互联网 发布:日本推理电影 知乎 编辑:程序博客网 时间:2024/06/16 17:18
1、在Java类中创建一个interceptor
首先在创建一个class类,继承AbstractInterceptor的抽象类,实现intercept方法
public class MyInterceptor extends AbstractInterceptor {@Overridepublic String intercept(ActionInvocation arg0) throws Exception {return null;}}
2、在Struts.xml文件中进行配置
A)、定义拦截器
<interceptors><interceptor name="拦截器名" class="拦截器的包名"></interceptor><interceptor-stack name="拦截器栈名"><interceptor-ref name="defaultStack"></interceptor-ref><interceptor-ref name="拦截器名"></interceptor-ref></interceptor-stack></interceptors>
Ps:defaultStack是默认的拦截器名称,不可更改
B)、使用拦截器
<default-interceptor-ref name="拦截器栈名"></default-interceptor-ref>
除此之外,还有一种不定义拦截器栈的方式
A)、定义拦截器
<interceptors><interceptor name="拦截器名" class="拦截器的包名"></interceptor></interceptors>
B)、使用拦截器
<action name="test" class="com.zuxia.action.TestAction"><interceptor-ref name="defaultStack"></interceptor-ref><interceptor-ref name="拦截器名"></interceptor-ref><result>/success.jsp</result></action>
注意:
1、在自定义拦截器后,默认的拦截器将会失效;
2、拦截器采用的是就近原则,先从局部查找,然后才是全局。
3、 指定拦截器
指定拦截器,和普通的拦截器大同小异
首先,创建一个class类,继承MethodFilterInterceptor抽象类,实现其方法
public class MeInterceptor extends MethodFilterInterceptor {@Overrideprotected String doIntercept(ActionInvocation arg0) throws Exception {System.out.println("这是指定拦截");arg0.invoke();return null;}}
然后,在struts.xml文件中配置,配置起来也和原来的差不多
<interceptor name="MeInterceptor" class="com.test.intercept.MeInterceptor"><param name="includeMethods">add,update</param> //<span style="font-family: 宋体;">这是用指定那些拦截器可用</span><param name="excludeMethods">add,update</param>//<span style="font-family: 宋体;">这是指那些拦截器不可用</span></interceptor>
4、重复表单提交
a) 、跳转的时候,不再用转发的方式,用重定向;
b) 、使用token拦截器
1、在JSP页面中,使用Struts2的标签库
<%@ taglib uri="/struts-tags" prefix="s"%>
然后在form表单中使用<s:token>标签
<form action="/Struts2_test/tokntest"> <s:token></s:token> 测试:<input type="text" name="test"> <input type="submit" value="提交"></form>
2、在struts.xml文件中配置
<action name="tokntest" class="com.test.action.TokeAction"><result name="invalid.token">/error.jsp</result><result>/success.jsp</result></action>
2 0
- 【Struts2】Struts2中拦截器
- struts2拦截器
- Struts2拦截器
- struts2.0拦截器
- struts2.0 拦截器
- struts2拦截器学习
- struts2 拦截器
- Struts2拦截器(Interceptor)
- struts2.0拦截器
- 用上struts2拦截器
- struts2拦截器简介
- Struts2:拦截器概述
- Struts2拦截器(Interceptor)
- struts2 拦截器
- 关于struts2拦截器
- Struts2的拦截器
- struts2拦截器
- Struts2拦截器总结
- Java 线程实现C/S会话
- IMP-00013: only a DBA can import a file exported by another DBA 问题及解决方案
- Swift傻傻分不清楚系列(四)基本运算符 o_O!???
- HTTP 协议入门知识
- Cypher查询语言--Neo4j中的SQL(5)
- Struts2拦截器
- oracle创建用户ORA-01045:user lacks CREATE SESSION privilege;logon denied..的问题
- 解决IOS在网页摇一摇会出现撤销弹出框的一种方案带音效的哈
- Swift中如何通过KVC将自定义的UITabbar设置为UITabbarController的tabbar
- jvm内存模型(运行时数据区)
- Cypher查询语言--Neo4j中的SQL(6)
- PHP生成验证码
- maven jar包不能下载,解决办法(.jar.lastUpdated)
- Jlink 刷新固件相关问题