2014-07-23 61 views
1

我通過這個 Struts2 Validation for an array如何:Struts 2個的驗證動態表單陣列

閱讀,它是有道理的,但希望人(四元)將解釋如何

「重寫上面的具體姓名字段(與索引)在這種情況下,你可以使用,你會使用addFieldError方法要詳細瞭解這些標籤看http://struts.apache.org/2.3.1.2/docs/tag-reference.html

這是我有:

<s:form action="saveOrUpdateAction" method="get"> 
    <s:token/> 
    <table> 
     <tr> 
      <td> Fund </td> 
      <td> Award Code </td> 
     </tr> 
     <s:iterator value="gfeListWithEmptyCode"> 
      <tr> 
       <td> <s:property value="sfafund "/> </td> 
       <td> <s:property value="awardcode"/> 
        <input type="text" name="codeArray"> 
       </td> 
      </tr> 
     </s:iterator> 
     <s:token /> 
     <s:submit key="Submit2"/> 
    </table> 
</s:form> 

我的行動的一部分:

public void validate() 
{ 
    if (fund == null || fund.trim().length() != 5) 
    { 
     System.out.println("testing+++++++++++++++++++1"); 
     addFieldError("fund","Fund requires 5 characters."); 
    } 
    if (code == null || code.trim().length() != 3) 
    { 
     System.out.println("testing+++++++++++++++++++2"); 
     addFieldError("code","Fund requires 3 characters."); 
    } 


    if (gfeListWithEmptyCode !=null) 
    { 

     int index = 0; 
     for (GiftFundEntity giftFundEntity : gfeListWithEmptyCode) 
     { 
      if (codeArray[index]!=null && codeArray[index].length() < 3) 
      { 

       System.out.println("testing+++++++++++++++++++3"); 

       // technically, this is not possible to do because it requires codeArray[index] and not a string. 

       addFieldError("codeArray","Code requires 3 characters."); 
       index++; 
      } 
     } 
    } 


    try 
    { 
     this.execute(); 
    } catch (Exception e) 
    { 
     e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. 
    } 
} 

在驗證過程中,紅色的錯誤信息不會出於顯而易見的原因,因爲codeArray不被索引中列出的JSP頁面上顯示出來。我如何得到這個工作? *請注意*該陣列是動態的。

我查看了struts文檔,並通過了stackoverflow搜索,但我不明白它是如何做到的。

謝謝你的時間。

+0

顯示你的jsp代碼? – hari

回答

0

答案是Struts 2將Action類中的codeArray變量視爲Array:String [] codeArray;這沒有記錄。