Struts2拦截器的使用
如何使用struts2拦截器,或者自定义拦截器。特别注意,在使用拦截器的时候,在Action里面必须最后一定要引用struts2自带的拦截器缺省堆栈defaultStack,如下(这里我是引用了struts2自带的checkbox拦截器):
<interceptor-ref name="checkbox">
也可以改为对全局Action设置自己需要的拦截器,如下:
在struts.xml里面定义全局的配置设置 |
<package name="struts-shop" extends="struts-default">
<interceptors>
<interceptor-stack name="myStack">
<interceptor-ref name="checkbox">
<param name="uncheckedValue">0</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack"/>(这句是设置所有Action自动调用的拦截器堆栈)
</package>
struts-action.xml里面配置Action如下:
<package name="LogonAdmin" extends="struts-shop">(这里扩展struts.xml里面定义的配置就可以了)
<action name="logon" class="logonAction">
<result>/jsp/smeishop/admin/index.jsp</result>
<result name="error">/jsp/smeishop/admin/logon.jsp</result>
<result name="input">/jsp/smeishop/admin/logon.jsp</result>
</action>
<action name="logout" class="logoutAction">
<result>/jsp/smeishop/admin/logon.jsp</result>
</action>
</package>
Struts2 拦截器 [Interceptor]
拦截器的工作原理如上图,每一个Action请求都包装在一系列的拦截器的内部。拦截器可以在Action
执行直线做相似的操作也可以在Action执行直后做回收操作。
的情况下所有的Action都被这个拦截器拦截。
使用这个方法可以给拦截器类做必要的初始话操作。
invocation.invoke()方法即可,在该方法调用的前后可以插入Action调用前后拦截器需要做的方法。
如果不需要调用后续的方法,则返回一个String类型的对象即可,例如Action.SUCCESS。
如果不存在,中止后续操作定位到LOGIN,否则执行原定操作,代码为:
拦截器
|
名字
|
说明
|
Alias Interceptor
|
alias
|
在不同请求之间将请求参数在不同名字件转换,请求内容不变
|
Chaining Interceptor
|
chain
|
让前一个Action的属性可以被后一个Action访问,现在和chain类型的result(<result type=”chain”>)结合使用。
|
Checkbox Interceptor
|
checkbox
|
添加了checkbox自动处理代码,将没有选中的checkbox的内容设定为false,而html默认情况下不提交没有选中的checkbox。
|
Cookies Interceptor
|
cookies
|
使用配置的name,value来是指cookies
|
Conversion Error Interceptor
|
conversionError
|
将错误从ActionContext中添加到Action的属性字段中。
|
Create Session Interceptor
|
createSession
|
自动的创建HttpSession,用来为需要使用到HttpSession的拦截器服务。
|
Debugging Interceptor
|
debugging
|
提供不同的调试用的页面来展现内部的数据状况。
|
Execute and Wait Interceptor
|
execAndWait
|
在后台执行Action,同时将用户带到一个中间的等待页面。
|
Exception Interceptor
|
exception
|
将异常定位到一个画面
|
File Upload Interceptor
|
fileUpload
|
提供文件上传功能
|
I18n Interceptor
|
i18n
|
记录用户选择的locale
|
Logger Interceptor
|
logger
|
输出Action的名字
|
Message Store Interceptor
|
store
|
存储或者访问实现ValidationAware接口的Action类出现的消息,错误,字段错误等。
|
Model Driven Interceptor
|
model-driven
|
如果一个类实现了ModelDriven,将getModel得到的结果放在Value Stack中。
|
Scoped Model Driven
|
scoped-model-driven
|
如果一个Action实现了ScopedModelDriven,则这个拦截器会从相应的Scope中取出model调用Action的setModel方法将其放入Action内部。
|
Parameters Interceptor
|
params
|
将请求中的参数设置到Action中去。
|
Prepare Interceptor
|
prepare
|
如果Acton实现了Preparable,则该拦截器调用Action类的prepare方法。
|
Scope Interceptor
|
scope
|
将Action状态存入session和application的简单方法。
|
Servlet Config Interceptor
|
servletConfig
|
提供访问HttpServletRequest和HttpServletResponse的方法,以Map的方式访问。
|
Static Parameters Interceptor
|
staticParams
|
从struts.xml文件中将<action>中的<param>中的内容设置到对应的Action中。
|
Roles Interceptor
|
roles
|
确定用户是否具有JAAS指定的Role,否则不予执行。
|
Timer Interceptor
|
timer
|
输出Action执行的时间
|
Token Interceptor
|
token
|
通过Token来避免双击
|
Token Session Interceptor
|
tokenSession
|
和Token Interceptor一样,不过双击的时候把请求的数据存储在Session中
|
Validation Interceptor
|
validation
|
使用action-validation.xml文件中定义的内容校验提交的数据。
|
Workflow Interceptor
|
workflow
|
调用Action的validate方法,一旦有错误返回,重新定位到INPUT画面
|
Parameter Filter Interceptor
|
N/A
|
从参数列表中删除不必要的参数
|
Profiling Interceptor
|
profiling
|
通过参数激活profile
|
要想Action执行,必须执行拦截器堆栈中的每一个拦截器。
统一定义所有的参数,例如:
或者
转载于:https://blog.51cto.com/wellwy/737841
Struts2拦截器的使用相关推荐
- Struts2拦截器属性excludeMethods、includeMethods配置无效之解决方法
参考:http://blog.csdn.net/coolcoffee168/article/details/7963251 在配置struts2 拦截器属性excludeMethods.include ...
- Struts2 拦截器 及如何获得 servlet 请求对象 以及Struts 基本配置 Session 超时设置...
在拦截器中可以三种实现 一:继承 AbstractInterceptor 类 二:继承 MethodFilterInterceptor类 三:实现 Interceptor 接口 在实现Intercep ...
- 基于struts2拦截器实现用户操作日志记录
2019独角兽企业重金招聘Python工程师标准>>> 这里基于struts2的拦截器来实现. 使用struts2拦截器拦截所有或者指定的请求,对用户操作过程中的:操作用户,操作时间 ...
- Struts2拦截器实现异常处理机制
http://bbs.itcast.cn/thread-10364-1-1.html Struts2拦截器实现异常处理机制 在j2ee项目中,系统内部难免会出现一些异常,如果把异常放任不管直接打印 ...
- 从struts2拦截器到自定义拦截器
http://www.cnblogs.com/withyou/p/3170440.html 拦截器可谓struts2的核心了,最基本的bean的注入就是通过默认的拦截器实现的,一般在struts2.x ...
- Struts2拦截器的使用 (详解)
Struts2拦截器的使用 (详解) 如何使用struts2拦截器,或者自定义拦截器.特别注意,在使用拦截器的时候,在Action里面必须最后一定要引用struts2自带的拦截器缺省堆栈default ...
- struts2 拦截器_Struts2令牌拦截器示例
struts2 拦截器 Struts 2 token interceptor can be used to handle multiple form submission problem. While ...
- struts2 拦截器_Struts2 execAndWait拦截器示例,用于长时间运行的动作
struts2 拦截器 Sometimes we have long running actions where user will have to wait for final result. In ...
- struts2 拦截器_Struts 2拦截器示例
struts2 拦截器 Welcome to Struts 2 Interceptor Example. While working on Struts 2, most of the time you ...
最新文章
- ios业务模块间互相跳转的解耦方案
- Boost:不受约束的bimap双图的测试程序
- decltype 和 auto
- Django从理论到实战(part5)--创建我们的第一个应用
- position:fixed 兼容浏览器低版本
- js室内地图开发_GIS开发_OpenLayers
- SAP License:SAP订单的归档及删除
- 2021级计科新生C语言项目设计视频集锦
- lua html 转义字符,Lua中特殊字符过滤(UTF8编码)
- Julia也能做爬虫?可以,但没必要
- DIY01_NE555叮咚门铃
- HFSS印刷偶极子天线(1)
- Linux基础(三)
- RAKsmart日本服务器的综合性能评测
- Spring 官方文档(中文翻译)
- 创业失败那天我在做什么
- Latex 绘制函数图像
- 通过Python爬取QQ空间说说并通过Pyechart进行可视化分析
- 申请阿里云的免费SSL证书,实现小程序HTTPS请求
- 不要放过那个装X的阿里,androidstudio基础