Struts2 Interceptor post processing

862 views Asked by At

I written custom interceptor and while post processing I want to add actionError/actionMessage by checking whether exception object exist or not on Value Stack.

 public String intercept(ActionInvocation invocation) throws Exception {

  result= invocation.invoke();

 // postprocessing start 

   invocation.addPreResultListener(new PreResultListener() {

  @Override
  public void beforeResult(ActionInvocation invocationInner, String resultCode) {

   Object obj=invocation.getInvocationContext().getValueStack().findValue("exception");
     if(obj!=null)
     {

         if(obj instanceof Exception)
         {
            try{
                  Object action=invocation.getAction();
                      ((ActionSupport)action).addActionError("Error");
               }

            catch (Exception e) {
                e.printStackTrace();
              }

             }

       }            



 }

 return result;

If result is "error" I am redirecting to error.jsp

 <s:if test="hasActionErrors()">
   <s:actionerror/>
 </s:if>

but nothing is displayed on error.jsp

even I am trying to set parameters from interceptor like

   invocationInner.getInvocationContext().getValueStack().setValue("errorNum", "333");

or

    Map<String, Object> parameters =    invocationInner.getInvocationContext().getParameters();
             parameters.put("errorNum", "8888");
             invocationInner.getInvocationContext().getValueStack().push(parameters);

and accessing parameter on jsp like

<s:property value="%{#parameters['errorNum']}"/> 

but no effect I am still getting nothing on jsp is it due to scope of object because when I am setting variable in session its working fine.

whether my approach to implement post processing is wrong or is anything I am missing.

0

There are 0 answers