2015-05-12 34 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代碼? –