2015-05-12 29 views
3

我已经尝试在Action类中添加操作错误并将它们打印到JSP页面上。动作错误不会显示在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> 

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> 

回答

1

当您执行redirectAction,一个新的请求被创建,因此所有的actionMessages,的ActionErrors,以及所有其它参数(未明确宣布要传递struts配置)丢失。

然后

  • 使用默认dispatcher结果,而不是redirectAction结果,或
  • 使用MessageStore Interceptor跨越重定向保留错误和消息,或
  • 返回类型调度的不同结果在出现错误的情况下,例如。 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; 
        } 
    } 
    
+0

您能否详细说明默认调度程序在我的代码中的用法。 直到现在我还没有使用它。 –

+0

从你的结果声明删除redirectAction,并指向一个JSP: '<结果名称=“成功”> result.jsp中' (如果没有指定结果类型,'类型=“调度员”假定' ) –

+0

但是,结果不应映射到jsp页面,它应该映射到struts的另一个操作,它将填充数据并显示生成的jsp页面。 –

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

这是怎么从OP代码有什么不同? –

+0

是否有任何理由或解释添加操作消息。 –

+0

什么是OP代码? –