struts2拦截器小结(2)

[b]1.类MethodFiledInterceptor[/b]

当只需要拦截特定方法的时候可以继承此拦截器。
然后配置文件中传入两个参数来实现过滤。

<interceptor-ref name="test">
<param name="excludeMethods">method1,method2</param>
<param name="includeMethods">method1,method3</param>
</interceptor-ref>

excludeMethods:不会被拦截器处理
includeMethods:会被拦截器处理
method1同时存在两个参数中,将会被归类为includeMethods.

[b]2.接口PreResutlListener[/b]

文件上传的时候,为action定义了一个"fileUpload"拦截器,

<interceptor-ref name="fileUpload" >
<param name="allowedTypes">application/msword,application/vnd.ms-excel,application/zip,text/plain</param>
<param name="maximumSize">5242880</param>
</interceptor-ref>

如果上传文件超过maximumSize指定大小或allowedTypes定义之外的文件类型,则框架会自动跳转到input视图,但是如果input视图里有一个用户列表,从数据库中读取数据,也就是说这个列表的数据必须经过另一个listUser请求才能得到并显示出来,很明显,框架的自动跳转input页面是不会去自动调用listUser请求的,这时候,input页面则显示的是一个没有数据的表格。
解决办法:为上传定一个自定义的拦截器myUpInterceptor,注册PreResultListener监听器(该监听器需要自己实现,功能是执行action之后返回result之前做点该做的事。

//这里,我将ManagerActionInterceptor类即当作拦截器又当作监听器
public class ManagerActionInterceptor extends MethodFilterInterceptor implements PreResultListener {

//实现监听器需实现的方法
public void beforeResult(ActionInvocation invocation, String resultCode) {
// TODO Auto-generated method stub

if(resultCode.equals("input")){
ManageAction ma=(ManageAction)invocation.getAction();
try {
//读取数据库,用于input页面显示
ma.userList();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

//实现拦截器需实现的方法
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
// TODO Auto-generated method stub
//注册监听器
invocation.addPreResultListener(new ManagerActionInterceptor());
return invocation.invoke();
}


[b]3.拦截器栈中多个参数写法[/b]


<interceptors>
<interceptor name="i1" class="test1">
<interceptor name="i2" class="test2">
<interceptor-stack name="i3">
<interceptor-ref name="i1"/>
<interceptor-ref name="i2"/>
</interceptor-stack>
</interceptors>

<action name="login" class="..">
<interceptor-ref name="i3">
<param name="i1.name">user</param>
<param name="i2.name">user</param>
</interceptor-ref>
</action>



[b]4.内建拦截器execAndWaite[/b]

如果一个action执行时间过长,如超过5秒,则可以使用此拦截器来实现用户等待页面的显示。

<action name="listUser" class="..">
<interceptor-ref name="execAndWait"/>
<interceptor-ref name="defaultStack"/>
<result name="wait">/waiting.jsp</result>
<result>/users.jsp</result>
</action>


waiting.jsp

<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<title></title>
<!--5秒后自动跳转到users.jsp页面 -->
<meta http-equiv="refresh" content="5;url=<s:url includeParams="all" />"/>
</head>
<body>
Please wait while we process your request.
Click [url=<s:url includeParams=]">[/url] if this page does not reload automatically.
</body>

</html>


[url="http://383984216-qq-com.iteye.com/admin/blogs/1151772][color=violet][b]"转载请注明出处:http://383984216-qq-com.iteye.com/admin/blogs/1151772[/b][/color][/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值