Action errors don't show up on JSP

I tried to add action errors to the Action class and print them to the JSP page.

When an exception is thrown, it goes into the catch block and prints the message "Error inserting exception, please contact your administrator" on the console.

In the catch block I added it using addActionError()

and I tried to print it to the jsp page ...
but the message is not showing on the jsp page .

What could I lose or do 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 print errors:

<s:if test="hasActionErrors()">
    <br></br>
    <div class="errors">
        <font color="red">
            <s:actionerror/>
        </font>
    </div>
</s:if>

      

+3


source to share


2 answers


When you execute redirectAction, a new request is created and therefore all actionMessages, actionErrors along with all other parameters (not explicitly declared in the struts configuration) are lost.

Then



  • Use the default dispatcher instead of redirectAction , or
  • use the MessageStore Interceptor to save errors and messages through redirects, or
  • returns a different type dispatcher result 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;
        }
    }
    
          

+1


source


Add an action message to the catch block, for example:

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

      



and then in 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>

      

+1


source







All Articles