Struts2的Action在实现com.opensymphony.xwork2.Preparable接口后,就可以重写prepare()方法此时在Action中,prepare()方法的执行点是在:setXxx()和execute()的执行之前比如需求:在执行Action的方法前,接收前台隐藏域传过来的值,再根据该值执行相应逻辑如前台传过来ID,我们根据ID查找数据库对应的用户信息,再跳转到modify()中修改信息但实际的运行过程中发现,通过Debug断点调试得知prepare()方法接收到的ID值是零即前台隐藏域中的ID值没有传过来,事实上问题就出在默认的defaultStack拦截器栈上其实defaultStack无法接收prepare()需要的数据,而应借助paramsPrepareParamsStack拦截器栈事实上使用prepare拦截器之前,应先调用params拦截器,prepare()才能接收到表单数据基于这个思路,于是可以通过各种手段将params拦截器放置在prepare拦截器之前即可比如将defaultStack中的所有拦截器拷贝到struts.xml的我们自定义的myStack拦截器栈中再按照paramsPrepareParamsStack拦截器栈中的params和prepare顺序修改二者位置即可。
1.拦截器类
- @SuppressWarnings("unchecked")
- public class TrainingInterceptor extends AbstractInterceptor{
- @Override
- public String intercept(ActionInvocation invocation) throws Exception {
- String result="nologin";
- logger.error("拦截器前");
- HttpServletRequest request = ServletActionContext.getRequest();
- HttpSession session = request.getSession();
- Object object=session.getAttribute("SALESMAN");
- if (object!=null) {
- result=invocation.invoke();
- }
- logger.error("拦截器后");
- return result;
- }
- }
- public class PublicAction extends ActionSupport implements Preparable{
- public PublicAction(){
- logger.error("bbb");
- }
- public void prepare() throws Exception {
- logger.error("aaaaaaa");
- }
情况1:action配置上拦截器输出日志:
09:53:40,788 ERROR [PublicAction] bbb
09:53:40,789 ERROR [TrainingInterceptor] 拦截器前
09:53:40,789 ERROR [TrainingInterceptor] 拦截器后
很显然prepare()方法没有走。
情况2:action去掉上拦截器输出日志:
09:56:42,999 ERROR [PublicAction] bbb
09:56:55,054 ERROR [PublicAction] aaaaaaa
这次prepare()方法走了。
3.推翻原有(上述观点)
原有观点:使用时特别注意:(自定义拦截器不能和此接口一起使用,否则此接口无效)
现有观点:原有观点错误,如下,我配置拦截器错误,原先是只配置了我自定义的拦截器,没有配置公共总的拦截器,所以就等于没有配置struts默认的拦截器,而Preparable接口的实现就是stuts2默认拦截器中的一个,所有就没有走,造成我原有的那种观点。现在好了,都走了,呵呵
- <interceptors>
- <!-- 社会渠道用户登录鉴权拦截器 -->
- <interceptor name="channellogon" class="cn.com.channel.member.interceptor.ChannelLogonInterceptior"/>
- <!-- 培训专区用户身份验证拦截器 -->
- <interceptor name="trainingInterceptor" class="cn.com.channel.ntraining.interceptor.TrainingInterceptor"></interceptor>
- <interceptor-stack name="channelintercepter">
- <interceptor-ref name="interceptorStack"></interceptor-ref>
- <interceptor-ref name="channellogon"></interceptor-ref>
- </interceptor-stack>
- <!-- 培训专区店员身份验证拦截器栈 -->
- <interceptor-stack name="trainingintercepterStack">
- <interceptor-ref name="channelintercepter"></interceptor-ref>
- <interceptor-ref name="trainingInterceptor"></interceptor-ref>
- </interceptor-stack>
- </interceptors>
- <default-interceptor-ref name="channelintercepter"></default-interceptor-ref>
- <global-results>
- <result name="nologin" type="chain">index</result>
- <result name="inputtrain" type="chain">totrain</result>
- </global-results>
- <!-- publics 培训专区公共 -->
- <action name="totrain" class="ntrainAction" method="gotoTrain">
- <result name="success">/page/channel/ntraining/train.jsp</result>
- <interceptor-ref name="trainingintercepterStack"></interceptor-ref>
- </action>
日志如下:走了
09:04:02,048 ERROR [TrainingInterceptor] interceptor befor....
09:04:03,132 ERROR [TrainingInterceptor] interceptor after....
转载于:https://blog.51cto.com/programmer/1124198