JSP页面上没有显示操作错误

3
我尝试在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
在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>

添加操作消息有什么原因或解释吗? - Raghu Markeffy
1
@RaghuMarkeffy:你在问题中提供的代码。 - Aleksandr M

1
当您执行redirectAction时,会创建一个新的请求,因此所有的actionMessages、actionErrors以及其他参数(未在struts配置中明确声明要传递的参数)都会丢失。
然后
  • 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;
        }
    }
    

请问您能否详细说明一下默认调度程序在我的代码中的使用方式。我目前还没有使用过它。 - Raghu Markeffy
但是,结果不应该映射到jsp页面,而应该映射到struts的另一个操作中,在那里它将填充数据并显示结果jsp页面。 - Raghu Markeffy
然后您需要使用MessageStore拦截器,或者在第一个操作中将数据放入会话中,并在第二个操作中自行从会话中检索(并清除会话)。请确保您需要在成功和错误的情况下映射另一个操作。 - Andrea Ligios
我无法理解MessageStore的概念。@Andrea Ligios - Raghu Markeffy
1
谢谢你的回答。你节省了很多时间。我学到了新的概念。 - Raghu Markeffy
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接