3

I have tried adding action errors in the Action class and printing them on the JSP page.

When an exception occurred, it is going into the catch block and it is printing "Error in inserting the Exception, Contact the Admin", in console.

In the catch block, I've added it with addActionError(), and I've tried printing it in jsp page...
but the message is not shown in jsp page.

What I may be missing or doing wrong ?

Struts mapping:

<action name="dataUpdate" class="foo.bar.myAction" method="updation">
    <result name="success" type="redirectAction">
        ../Aggregator/redirectToDataUpdate
    </result>
</action>

Action class:

public String updation() throws JiffieTransactionException{
    try {
        // do stuff...
    } catch (NumberFormatException e) {
        addActionError("Error in inserting the Exception, Contact the Admin");
        System.out.println("Error in inserting the Exception, Contact the Admin");
        e.printStackTrace();
    }
    return SUCCESS;
}

JSP code for printing action errors:

<s:if test="hasActionErrors()">
    <br></br>
    <div class="errors">
        <font color="red">
            <s:actionerror/>
        </font>
    </div>
</s:if>
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243

2 Answers2

1

Add an action message in catch block like :

addActionMessage("Error in inserting the Exception, Contact the Admin"); //in catch block

and then on jsp write:

<s:if test="hasActionErrors()">
  <br></br>
     <div class="errors">
       <font color="red">
              <s:actionerror/>
            </font>
     </div>
   <s:if test="hasActionMessages()">
     <div class="errors">
       <font color="red">
          <s:actionmessage/>
       </font>
      </div>
   </s:if>
  </s:if>
Brijesh Bhatt
  • 3,810
  • 3
  • 18
  • 34
1

When you perform a redirectAction, a new Request is created, and hence all the actionMessages, actionErrors, along with all the other parameters (not expressly declared to be passed in the struts config) are lost.

Then

  • Use a default dispatcher result instead of a redirectAction result, or
  • use the MessageStore Interceptor to preserve errors and messages across the redirects, or
  • return a different result of type dispatcher in case of errors, eg. ERROR:

    <action name="dataUpdate" class="foo.bar.myAction" method="updation">
        <result name="success" type="redirectAction">....redirectToDataUpdate</result>
        <result name="error">previousPage.jsp</result>
    </action>
    
    public String updation() {
        try {
            // do stuff...
            return SUCCESS;
        } catch (NumberFormatException e) {
            addActionError("Errors... ");
            e.printStackTrace();
            return ERROR;
        }
    }
    
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Can you please elaborate the usage of default dispatcher to my code. I have not used it till now. – Raghu Markeffy May 12 '15 at 09:00
  • Remove the redirectAction from your result declaration, and point to a JSP: `result.jsp` (if no result type is specified, `type="dispatcher"` is assumed) – Andrea Ligios May 12 '15 at 09:02
  • But, the result should not to be mapped to a jsp page, it should map to another action of struts, where it will populate data and display the resultant jsp page. – Raghu Markeffy May 12 '15 at 09:09
  • Then you need to use the MessageStore Interceptor, OR to put the data in session in the first action and retrieve it from the session (and clear the session) in the second action by yourself. Please ensure that you need to map another action both in case of success and in case of error – Andrea Ligios May 12 '15 at 09:11
  • In case you want to use MessageStore, take a look at this question: http://stackoverflow.com/questions/13414543/struts-2-validation-using-message-store-interceptor – Andrea Ligios May 12 '15 at 09:13
  • I was unable to understand the MessageStore concept. @Andrea Ligios – Raghu Markeffy May 12 '15 at 09:37
  • In a nutshell, if you can't pass items between ActionA and ActionB through the Request (that is lost in a redirectAction), you must use the Session. You can do it both manually, or with the help of the MessageStore Interceptor. The operation is always the same: 1) STORE the message in Session in ActionA 2) RETRIEVE the message from the Session in ActionB. Then 3) decide where/when to REMOVE the message from the session; if you do it after retrieving it, you will see the message only once (eg. not after an F5), otherwise you will risk to see it every time you access ActionB,even when not needed – Andrea Ligios May 12 '15 at 09:46
  • 1
    Thanks for your answer. You saved a lot of time. I learned new concept. – Raghu Markeffy May 13 '15 at 06:49