扩展拦截器

来源:互联网 发布:js中鼠标点击事件 编辑:程序博客网 时间:2024/06/07 15:34

扩展拦截器 http://www.dianzhangbbs.com 
你可以扩展这个拦截器和覆盖#acceptFile方法提供更多控制,支持的文件和不支持的文件.
例子
<action name="doUpload" class="com.examples.UploadAction">
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="basicStack"/>
<result name="success">good_result.ftl</result>
</action>
</pre>
And then you need to set encoding <code>multipart/form-data</code> in the form where the user selects the file to upload.
<pre>
<a:form action="doUpload" method="post" enctype="multipart/form-data">
<a:file name="upload" label="File"/>
<a:submit/>
</a:form>
</pre>
And then in your action code you'll have access to the File object if you provide setters according to the
naming convention documented in the start.
<pre>
public com.examples.UploadAction implemements Action {
private File file;
private String contentType;
private String filename;
public void setUpload(File file) {
this.file = file;
}
public void setUploadContentType(String contentType) {
this.contentType = contentType;
}
public void setUploadFileName(String filename) {
this.filename = filename;
}
...
}
</pre>
设置参数的例子:
<interceptor-ref name="fileUpload">
<param name="allowedTypes">
image/png,image/gif,image/jpeg
</param>
</interceptor-ref>

I18n Interceptor
一个拦截器处理设置指定会话中的locale为当前的Action请求.另外,这个拦截器将查找指定的HTTP请求参数和设置locale使用提供的任何值.这意味着拦截器可以让你的应用程序动态的改变locale为用户的会话.这非常有用对应用程序想要支持多种语言和用户设置他或她的语言.locale参数被移除在拦截器执行期间,确保属性不被设置在Action上(像request_locale),不是特有的setter在Action中.
例如,使用默认的参数名,一个请示到foo.action?request_locale=en_US,locale美式英语被保存到用户的会话中并且将用于所有的后面请求中.
参数
.parameterName(可选)-HTTP请求的参数名,locale作为开关和保存会话中,默认为request_locale
.attributeName(可选)-session key的名字存储选中的locale.默认是WW_TRANS_I18N_LOCALE
扩展拦截器
这个拦截器没有扩展点
例子
<action name="someAction" class="com.examples.SomeAction">
<interceptor-ref name="i18n"/>
<interceptor-ref name="basicStack"/>
<result name="success">good_result.ftl</result>
</action>


Logger Interceptor
拦截器日志开始和结束Action的执行(仅英文,非国际化).
参数 这个拦截器没有参数.
扩展拦截器 没有明显的扩展到存在的拦截器.
例子
<!-- prints out a message before and after the immediate action execution -->
<action name="someAction" class="com.examples.SomeAction">
<interceptor-ref name="completeStack"/>
<interceptor-ref name="logger"/>
<result name="success">good_result.ftl</result>
</action>
<!-- prints out a message before any more interceptors continue and after they have finished -->
<action name="someAction" class="com.examples.SomeAction">
<interceptor-ref name="logger"/>
<interceptor-ref name="completeStack"/>
<result name="success">good_result.ftl</result>
</action>


Message Store Interceptor
一个拦截器存储ValidationAware Action的消息/错误和字段错误到Http Session,这样它将被重新获取在最后阶段.这允许Action的消息/错误和字段错误长期有效在特定的http请求.
在’STORE’模式中,拦截器将重新获取存储的Action 消息/错误和字段错误并且放他们到ValidationAware Action.
拦截器在’NONE’模式中什么也不做,其中一个是默认的.
操作模式可以被交换使用:
1]设置拦截器参数例子.
<action name="submitApplication" ...>
<interceptor-ref name="store">
<param name="operationMode">STORE</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
....
</action>
2]通过请求参数(allowRequestParameterSwitch 必需为’true’,是默认的)
// the request will have the operation mode in 'STORE'
http://localhost:8080/context/submitApplication.action?operationMode=STORE
参数
. allowRequestParameterSwitch-开启请求参数可以交换拦截器的操作模式.
. requestParameterSwitch-指示拦截器中使用什么样的请求参数.
.operationMode-这个拦截器使用的操作模式(‘STORE’,’RETRIEVE’或’NONE’其中一个).’NONE’为默认.
扩展拦截器
下面的方法将被覆盖:
. getRequestOperationMode-获取拦截器的操作模式基于请求参数.
.mergeCollection-合并两个集合.
.mergeMap – 合并两个map
例子
<action name="submitApplication" ....>
<interceptor-ref name="store">
<param name="operationMode">STORE</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
<result name="input" type="redirect">applicationFailed.action</result>
<result type="dispatcher">applicationSuccess.jsp</result>
</action>
<action name="applicationFailed" ....>
<interceptor-ref name="store">
<param name="operationMode">RETRIEVE</param>
</interceptor-ref>
<result>applicationFailed.jsp</result>
</action>

同上面的例子, 'submitApplication.action'有Action消息/错误/字段错误存储到Http会话中.以后需要的时候,在这种情况下,’applicationFailed.action’被激活,它将获取Action 消息/错误/字段错误存储在HTTP会话中并且放回到Action.

Model Driven Interceptor
查看ModelDriven Action和添加Action的模型到值栈.
注意: ModelDrivenInterceptor必需出现在StaticParametersInterceptor和ParametersInterceptor之前,如果你想要参数被应用到模型.
注意: ModelDrivenInterceptor将仅放模型到栈当模型不为null,其它的的将被忽略.
参数 None
扩展拦截器 这个拦截器没有扩展点.
例子
<action name="someAction" class="com.examples.SomeAction">
<interceptor-ref name="model-driven"/>
<interceptor-ref name="basicStack"/>
<result name="success">good_result.ftl</result>
</action>


Scoped Model Driven Interceptor
一个开授权给范围模型驱动的Action的拦截器.
这个拦截器仅激活实现ScopeModelDriven接口的Action.如果被检查到.,它将重新获取模型类从配置范围内,这样提共它到Action.
参数
.calssName-模型类的名字.默认为通过getModel()方法返回的对象的类名.
.name-关键字用来使用当存储或重获实例在一个范围内。默认为模型类名.
.scope-存储或重获模型的范围.默认为’request’也可以是’session’.
扩展拦截器 此拦截器无扩展点
例子
<-- Basic usage -->
<interceptor name="scoped-model-driven" class="com.opensymphony.interceptor.ScopedModelDrivenInterceptor" />
<-- Using all available parameters -->
<interceptor name="gangsterForm" class="com.opensymphony.interceptor.ScopedModelDrivenInterceptor">
<param name="scope">session</param>
<param name="name">gangsterForm</param>
<param name="className">com.opensymphony.example.GangsterForm</param>
</interceptor>


Servlet Config Interceptor
一个拦截器设置Action属性基于接口实现的Action。例如,如果Action实现ParameterAware那么Action上下文的参数映射将会被设置.
这个拦截器设计来设置一有的属性,一个Action需要知道Servlet参数,servlet上下文,会话等等.接口支持:
ServletContextAware
ServletRequestAware
ServletResponseAware
ParameterAware
RequestAware
SessionAware
ApplicationAware
PrincipalAware
参数 无
扩展拦截器 此拦截器无扩展点。
例子
<action name="someAction" class="com.examples.SomeAction">
<interceptor-ref name="servlet-config"/>
<interceptor-ref name="basicStack"/>
<result name="success">good_result.ftl</result>
</action>


Static Parameters Interceptor
这个拦截器位于静态参数定义的Action配置文件中.如果Action实现Parameterizable,静态参数映射的Map将会被直接传递到Action.
参数典型的定义在<param>元素内存在于xwork.xml文件中.
参数 None
扩展拦截器 这个拦截器没有扩展点.
例子
<action name="someAction" class="com.examples.SomeAction">
<interceptor-ref name="static-params">
<param name="parse">true</param>
</interceptor-ref>
<result name="success">good_result.ftl</result>
</action>

Roles Interceptor
它被执行仅当用户有正确的角色.

Timer Interceptor
这个拦截器记录时间单位为毫秒.为了使这个拦截器正常的工作,日志框架必需被设置,至少是INFO级别.这个拦截器依赖于Commons Logging API 报告执行时间值.
参数
.logLevel(可选的)-我们使用什么日志级别(trace,debug,info,warn,error,fatal)? –默认为info
.logCategory(可选的)-假如我们使用这个类别(eg.com.mycompany.app).默认使用com.opensymphony.xwork2.interceptor.TimerInterceptor.
开启上面的参数我们记录所有的Action执行期间的日志到日志文件中.
扩展拦截器
这个拦截器可以被扩展提供定制的消息格式.用户可以覆盖invokeUnderTiming方法.
例子
<!-- records only the action's execution time -->
<action name="someAction" class="com.examples.SomeAction">
<interceptor-ref name="completeStack"/>
<interceptor-ref name="timer"/>
<result name="success">good_result.ftl</result>
</action>
<!-- records action's execution time as well as other interceptors-->
<action name="someAction" class="com.examples.SomeAction">
<interceptor-ref name="timer"/>
<interceptor-ref name="completeStack"/>
<result name="success">good_result.ftl</result>
</action>

Token Interceptor
确保仅有一个请求每个标记被处理.这个拦截器可以确保回退按钮和双击不会引起非预想到的影响.例如,你可以使用它来阻止粗心的用户双击一个”check out”按钮在一个在线商店中.这个拦截器使用一个完全原始的支术当一个无效的标记被找到;它将返回结果invalid.token,它可以被映射到你的Action配置中.一个更复杂的实现, TokenSessionStoreInterceptor,可以提供更好的逻辑当无效的标记被找到.
注意:设置一个token在你的表单中,你必需使用token标签.这个标签要求并且必需用于表单中,提交到Action保护通过这个拦截器.一些请求不提供token(使用token标签)将会被处理做为一个无效的token请求.
国际化注意事项:下面的key可以用于国际化Action错误生成通过这个token拦截器.
. struts.messages.invalid.token
注意:当这个方法离开MethodFilterInterceptor扩展时,它有能力决定选择Action中的方法,见MethodFilterInterceptor获取更多的信息.
参数 None
扩展拦截器
用户不经常扩展它,这个拦截器扩展通过TokenSessionStoreInterceptor. handleInvalidToken和handleValidToken方法保护和可用更多有趣的逻辑,像做token会话拦截.
例子
<action name="someAction" class="com.examples.SomeAction">
<interceptor-ref name="token"/>
<interceptor-ref name="basicStack"/>
<result name="success">good_result.ftl</result>
</action>
<-- In this case, myMethod of the action class will not
get checked for invalidity of token -->
<action name="someAction" class="com.examples.SomeAction">
<interceptor-ref name="token">
<param name="excludeMethods">myMethod</param>
</interceptor-ref name="token"/>
<interceptor-ref name="basicStack"/>
<result name="success">good_result.ftl</result>
</action>

Token Session Interceptor
拦截器不是从TokenInterceptor构建,提供先进的逻辑处理无效的token.不像普通token拦截器,这个拦截器将尝试提供智能的失败处理在多个请求事件请求使用相同的会话.那样,它将阻碍后面的请求,直到前一个请求完成,并且代替换返回invalid.token代码,它将尝试显示相同的原始资源,如果没有多个请求被提交在第一个地方,验证Action调用将会显示.
注意:像远离MethodFilterInterceptor方法,它能决定是仅选择适当的方法在Action类中,见来MethodFilterInterceptor获取更多的信息.
参数 None
扩展拦截器 这个拦截器没有扩展点.
例子
<action name="someAction" class="com.examples.SomeAction">
<interceptor-ref name="token-session/>
<interceptor-ref name="basicStack"/>
<result name="success">good_result.ftl</result>
</action>
<-- In this case, myMethod of the action class will not
get checked for invalidity of token -->
<action name="someAction" class="com.examples.SomeAction">
<interceptor-ref name="token-session>
<param name="excludeMethods">myMethod</param>
</interceptor-ref name="token-session>
<interceptor-ref name="basicStack"/>
<result name="success">good_result.ftl</result>
</action>

Validation Interceptor
这个拦截器运行Action通过标准的验证框架,轮流检查针对其它的验证规则(查找文件像ActionClass-validation.xml)和添加字段级和Action级错误消息(提供通过Action实现com.opensymphony.xwork2.ValidationAware),这个拦截器通常是最后一个(或仅次于最后一个)应用到栈中的拦截器,同样的假定所有的值已经被设置到Action.
如果方法名被调用指定在excludeMethods参数,拦截器什么也不做.excludeMethods接收一个逗号分隔的方法名列表.例如,请求foo!input.action和foo!back.action将会被跳过通过这个拦截器,如果你设置excludeMethods参数到”input,back”.
注意什么也不用做使用com.opensymphony.xwork2.Validateable接口并且简单的添加错误消息到Action.Action请求的流程不会改变原有的拦截器.宁原这个拦截器常常在conjuction同workflow拦截器使用.
注意:因为这个方法不是从MethodFilterInterceptor继承, 它能决定是仅选择适当的方法在Action类中,见来MethodFilterInterceptor获取更多的信息.
参数 None
扩展拦截器 这个拦截器无扩展点
例子
<action name="someAction" class="com.examples.SomeAction">
<interceptor-ref name="params"/>
<interceptor-ref name="validation"/>
<interceptor-ref name="workflow"/>
<result name="success">good_result.ftl</result>
</action>
<-- in the following case myMethod of the action class will not
get validated -->
<action name="someAction" class="com.examples.SomeAction">
<interceptor-ref name="params"/>
<interceptor-ref name="validation">
<param name="excludeMethods">myMethod</param>
</interceptor-ref>
<interceptor-ref name="workflow"/>
<result name="success">good_result.ftl</result>
</action>
http://hi.baidu.com/oush/blog/item/f908013816ae2cf1b311c721.html

原创文章如转载,请注明:转载自Scofield的blog [ http://jeplove.blog.zj.com/ ]
本文链接地址:http://jeplove.blog.zj.com/blog/d-233227.html


文章来自: 博客浙江(blog.zj.com)
原文参考:http://jeplove.blog.zj.com/d-233227.html

原创粉丝点击