在struts2的struts-default.xml中定义了一个name为prepare拦截器,实现类是com.opensymphony.xwork2.interceptor.PrepareInterceptor,它的作用是为实现了com.opensymphony.xwork2.Preparable接口的action调用相关方法。该拦截器有两个参数:alwaysInvokePrepare,firstCallPrepareDo,两者的类型都是boolean,默认值分别是true,false。
该拦截器的核心代码如下:
public String doIntercept(ActionInvocation invocation) throws Exception {
Object action = invocation.getAction();
if (action instanceof Preparable) {
try {
String[] prefixes;
if (firstCallPrepareDo) {
prefixes = new String[] {ALT_PREPARE_PREFIX, PREPARE_PREFIX};
} else {
prefixes = new String[] {PREPARE_PREFIX, ALT_PREPARE_PREFIX};
}
PrefixMethodInvocationUtil.invokePrefixMethod(invocation, prefixes);
}
catch (InvocationTargetException e) {
/*
* The invoked method threw an exception and reflection wrapped it
* in an InvocationTargetException.
* If possible re-throw the original exception so that normal
* exception handling will take place.
*/
Throwable cause = e.getCause();
if (cause instanceof Exception) {
throw (Exception) cause;
} else if(cause instanceof Error) {
throw (Error) cause;
} else {
/*
* The cause is not an Exception or Error (must be Throwable) so
* just re-throw the wrapped exception.
*/
throw e;
}
}
if (alwaysInvokePrepare) {
((Preparable) action).prepare();
}
}
return invocation.invoke();
}
该代码的逻辑非常简单,如果action实现了com.opensymphony.xwork2.Preparable接口,则在调用setXXX和execute()方法之前调用系列方法。如果firstCallPrepareDo为true,则调用prepareDoXXX方法,否则调用prepareXXX方法(XXX为action对应的方法)。接下来查看alwaysInvokePrepare状态,如果其值为true则调用com.opensymphony.xwork2.Preparable接口的prepare方法。最后将action转交给下一个拦截器。
版权所有,转载请标明出处:http://blogwarning.iteye.com/blog/1390172
分享到:
相关推荐
Struts2拦截器(Interceptor) Struts2拦截器(Interceptor)
Struts2默认拦截器解析.pdf
解决Struts2中的中文乱码。该代码是用作Struts2的拦截器中
struts2常用拦截器,struts2经常用到的拦截器,熟悉熟悉
Struts2默认拦截器解析[归类].pdf
该例子为struts2注解与拦截器demo,利用myEclipse8.5开发,导入刚才后,自动加载所需struts2的jar包,可以直接运行,是初学struts2注解、拦截器很好的例子,保证10分钟学会2种技术,愿意分享给大家。
struts2 Interceptor拦截器 http://whitewolf.0fees.net
使用Struts2实现用户权限拦截、重点是掌握拦截器的使用和配置方法,通过案例的学习,就能掌握struts中拦截器的使用方法,让我们能快速上手
Struts2拦截器.ppt Struts2拦截器.ppt Struts2拦截器.ppt
关于struts2 拦截器的详细的解释及其在开发中对struts2的拦截器的应用和拦截器在框架中的作用.
Struts2拦截器源程序 Struts2拦截器源程序 Struts2拦截器源程序 Struts2拦截器源程序 Struts2拦截器源程序
详细说明了struts2中拦截器的使用。并且通过一个小例子讲解了拦截器的具体使用
struts2,Interceptor struts2拦截器实例,两套实例,一套是针对单个的action配置的,另一套实例是针对全局的action配置的拦截器interceptor
struts2 用拦截器 实现用户权限登录 可以直接运行,只单单用到struts的东西。
基于struts2的拦截器测试,实现了页面的跳转,中间过程的拦截
struts2拦截器应用小例子,与大家共分享
Struts2拦截器实现权限控制demo,我感觉对初学者还是有一定帮助的
Struts2的拦截器的使用详解实例 博文链接:https://zmx.iteye.com/blog/457434
Struts2的核心,拦截器的应用!!!