Struts2Action在实现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拦截器栈中的paramsprepare顺序修改二者位置即可。

1.拦截器类

 
  
  1. @SuppressWarnings("unchecked"
  2. public class TrainingInterceptor  extends AbstractInterceptor{ 
  3.     @Override 
  4.     public String intercept(ActionInvocation invocation) throws Exception { 
  5.          String result="nologin"
  6.          logger.error("拦截器前"); 
  7.         HttpServletRequest request = ServletActionContext.getRequest(); 
  8.         HttpSession session = request.getSession(); 
  9.         Object object=session.getAttribute("SALESMAN"); 
  10.         if (object!=null) { 
  11.           result=invocation.invoke(); 
  12.         } 
  13.          logger.error("拦截器后"); 
  14.         return result; 
  15.     } 

2. 实现Preparable接口的action
 
  
  1. public class PublicAction extends ActionSupport implements Preparable{ 
  2. public PublicAction(){ 
  3.    logger.error("bbb"); 
  4. public void prepare() throws Exception { 
  5.    logger.error("aaaaaaa"); 

情况1action配置上拦截器输出日志:

09:53:40,788 ERROR [PublicAction] bbb

09:53:40,789 ERROR [TrainingInterceptor] 拦截器前

09:53:40,789 ERROR [TrainingInterceptor] 拦截器后

很显然prepare()方法没有走。

情况2action去掉上拦截器输出日志:

09:56:42,999 ERROR [PublicAction] bbb

09:56:55,054 ERROR [PublicAction] aaaaaaa

这次prepare()方法走了。

 3.推翻原有(上述观点)

原有观点:使用时特别注意:(自定义拦截器不能和此接口一起使用,否则此接口无效)

现有观点:原有观点错误,如下,我配置拦截器错误,原先是只配置了我自定义的拦截器,没有配置公共总的拦截器,所以就等于没有配置struts默认的拦截器,而Preparable接口的实现就是stuts2默认拦截器中的一个,所有就没有走,造成我原有的那种观点。现在好了,都走了,呵呵

 
   
  1. <interceptors> 
  2.       <!-- 社会渠道用户登录鉴权拦截器 --> 
  3.       <interceptor name="channellogon" class="cn.com.channel.member.interceptor.ChannelLogonInterceptior"/> 
  4.       <!-- 培训专区用户身份验证拦截器 --> 
  5.       <interceptor name="trainingInterceptor" class="cn.com.channel.ntraining.interceptor.TrainingInterceptor"></interceptor> 
  6.       <interceptor-stack name="channelintercepter"> 
  7.          <interceptor-ref name="interceptorStack"></interceptor-ref> 
  8.          <interceptor-ref name="channellogon"></interceptor-ref> 
  9.       </interceptor-stack> 
  10.       <!-- 培训专区店员身份验证拦截器栈 --> 
  11.       <interceptor-stack name="trainingintercepterStack"> 
  12.          <interceptor-ref name="channelintercepter"></interceptor-ref> 
  13.          <interceptor-ref name="trainingInterceptor"></interceptor-ref> 
  14.       </interceptor-stack> 
  15. </interceptors> 
  16. <default-interceptor-ref name="channelintercepter"></default-interceptor-ref> 
  17. <global-results> 
  18.   <result name="nologin" type="chain">index</result> 
  19.   <result name="inputtrain" type="chain">totrain</result> 
  20. </global-results> 
  21. <!-- publics 培训专区公共 --> 
  22. <action name="totrain" class="ntrainAction" method="gotoTrain"> 
  23.     <result name="success">/page/channel/ntraining/train.jsp</result> 
  24.     <interceptor-ref name="trainingintercepterStack"></interceptor-ref> 
  25. </action>  

日志如下:走了

09:04:02,047 ERROR [PublicAction] prepare....
09:04:02,048 ERROR [TrainingInterceptor] interceptor befor....
09:04:03,132 ERROR [TrainingInterceptor] interceptor after....