2015-05-27 17 views
0

林迭代與Struts的工作,需要使用此代碼迭代使用邏輯:用String數組

<logic:iterate name="myForm" id="syncStringArrayId" property="list"> 
      <tr>    
       <td><bean:write name="syncStringArrayId" /></td> 
       <td>2nd column should be index 1 of string array</td> 
       <td>3rd column should be index 2 of string array</td> 
      </tr> 
</logic:iterate> 

遍歷字符串數組列表即

List<String[]> list = new ArrayList<String[]>(); 

IM但是當我執行該代碼它打印:

Column1      Colmn2  Column3 
[Ljava.lang.String;@2803cc Col2-Data Col3-Data/value 

表示它是打印完整的字符串數組,但我無法打印字符串數組的索引。

任何人都可以幫我找到我的錯誤。

編輯

請告知,如果這是根本不可能的,因爲我可以用一些豆才達到同樣的事情,就像

List<someBean> list = new ArrayList<someBean>(); 

和數據即時通訊將在字符串數組我會救保存在bean中,這將工作。

+1

使用索引從數組中檢索值。 –

+0

@AleksandrM我嘗試過使用索引,但沒有得到確切的想法,請你多解釋一下。 – NoNaMe

+0

所以,你的實際問題是「我有一個String []類型的屬性,我想訪問它的一個元素」。如果struts不支持'syncStringArrayId [0]'(我不知道,自從我使用它已經很長時間了),使用EL(而不是'',使用' $ {syncStringArrayId [0]}'。 – SJuan76

回答

0

這裏是LIB解決我的問題

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 

我用foreach循環遍歷列表這樣。

<c:forEach items="${myForm.list}" var="bean"> 
      <tr> 
       <td><c:out value="${bean[0]}" /></td> 
       <td><c:out value="${bean[2]}" /></td> 
       <td><c:out value="${bean[1]}" /></td> 
      </tr> 
     </c:forEach> 

希望這會幫助某人,謝謝大家的幫助。

0

您的列表將字符串數組存儲爲對象ie List list = new ArrayList();所以當您遍歷List時,它會給您String [],這將使實習生包含數量的元素,因爲它是一個數組而不是單個元素並且您將需要再應用一個循環來打印來自string []的值。 希望你明白我的意思。

+0

不,不需要其他循環,因爲我可以通過索引[0],索引[1] ...等獲取String []的值,這不是我的問題 – NoNaMe

0

看起來,你已經重複了List,但錯過了迭代String array

能不能請你:

<table> 
    <logic:iterate name="myForm" id="syncStringArrayId" property="list" indexId="indexId"> 
    <tr> 
     <td><bean:write name="indexId" /></td> 
     <logic:iterate name="syncStringArrayId" id="strValue"> 
      <td><bean:write name="strValue" /></td> 
     </logic:iterate>   
    </tr> 
    </logic:iterate> 
</table>