简体   繁体   English

JSP上未显示操作错误

[英]Action errors are not shown on the JSP

I have tried adding action errors in the Action class and printing them on the JSP page. 我尝试在Action类中添加操作错误并在JSP页面上打印它们。

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. 发生异常时,它将进入catch块,并在控制台中打印“插入异常,联系管理员时出错”。

In the catch block, I've added it with addActionError() , and I've tried printing it in jsp page... 在catch块中,我添加了addActionError() ,我尝试在jsp页面中打印它...
but the message is not shown in jsp page . 但该消息未在jsp页面中显示

What I may be missing or doing wrong ? 我可能遗失或做错了什么?

Struts mapping: Struts映射:

<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: 用于打印操作错误的JSP代码:

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

Add an action message in catch block like : 在catch块中添加一条动作消息,如:

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

and then on jsp write: 然后在jsp上写:

<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>

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. 当您执行redirectAction时,会创建一个新的Request,因此所有的actionMessages,actionErrors以及所有其他参数(未明确声明在struts配置中传递)都将丢失。

Then 然后

  • Use a default dispatcher result instead of a redirectAction result, or 使用默认调度程序结果而不是redirectAction结果,或
  • use the MessageStore Interceptor to preserve errors and messages across the redirects, or 使用MessageStore Interceptor来保留重定向中的错误和消息,或者
  • return a different result of type dispatcher in case of errors, eg. 如果出现错误,则返回类型调度程序的不同结果,例如。 ERROR : 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; } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM