有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java操作错误不会显示在JSP上

我曾尝试在action类中添加操作错误,并在JSP页面上打印它们

当异常发生时,它将进入catch块,并在控制台中打印“插入异常时出错,请与管理员联系”

在catch块中,我添加了addActionError(),并尝试在jsp页面中打印它。。。
但该消息未显示在jsp页面中

我可能错过了什么或做错了什么

Struts映射:

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

动作类:

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代码:

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

共 (2) 个答案

  1. # 1 楼答案

    在catch块中添加一条操作消息,如:

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

    然后在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>
    
  2. # 2 楼答案

    执行重定向操作时,会创建一个新请求,因此所有actionMessages、actionErrors以及所有其他参数(未明确声明在struts配置中传递)都会丢失

    然后

    • 使用默认的dispatcher结果而不是redirectAction结果,或者
    • 使用MessageStore Interceptor保留重定向中的错误和消息,或
    • 如果出现错误,则返回类型为dispatcher的不同结果,例如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;
          }
      }