2012-02-08 50 views
0

我在JSP中使用<s:iterator>標籤來顯示List人物對象。如何在JSP中顯示動作數據?

我嘗試在動作類中創建ListOfPersons作爲List以及getter和setter。我仍然無法顯示數據 - 我該怎麼做?

<s:iterator value="ListOfpersons" status="stat"> 

當我嘗試打印列表的大小時,我得到零。

+0

我還不確定你在說哪個標籤?是一個自定義標籤還是它的'',請張貼行動代碼類以及標籤的詳細信息。 – 2012-02-09 03:32:34

回答

0

如果我理解你的話,你在使用JSP頁面上的OGNL表達列表時遇到問題。您應該用YourListName[index].property格式命名字段。然後OGNL會明白你有一個名爲YourListName的列表,其元素index的值爲property,其值爲輸入。

請看下面的例子:

<table> 
<s:iterator value="ListOfpersons" status="status"> 
<tr> 
    <td><s:textfield name="ListOfpersons[%{#status.index}].firstname"/></td> 
    <td><s:textfield name="ListOfpersons[%{#status.index}].lastname"/></td> 
    <td><s:textfield name="ListOfpersons[%{#status.index}].age"/></td> 
    <td><s:textfield name="ListOfpersons[%{#status.index}].sex"/></td> 
</tr> 
</s:iterator> 
</table> 
+2

我相信,如果List對象不是Map,那麼可以很容易地在JSP中引用List對象。因爲Object將被放置在堆棧的頂部,並且您可以像'firstName'等那樣輕鬆地引用它。 – 2012-02-09 04:59:10

+0

@ UmeshAwasthi您能否給我示例? – batbaatar 2012-02-09 05:24:08

+0

請參閱文檔中的示例。 http://www.struts.apache.org/2.3.1/docs/iterator.html – 2012-02-09 05:42:11

0

答案很簡單:存儲在JSP的請求,並訪問其中的信息。

再回應:

  1. 創建於servlet的一些對象(在你的情況下,動作)。
  2. 將對象存儲在某個JSP作用域(HttpServletRequest.setAttribute())中。
  3. 將請求轉發(調度)到JSP頁面(這只是struts配置,你已經這樣做了)。
  4. 在JSP頁面中,引用變量(可能使用c:out標記或僅在JSP頁面文本中使用EL表達式)。

一些代碼(支柱的1.x):

class Blah extends Action 
{ 
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 
    { 
    ... do stuff 
    request.setAttribute("Blammy", "Blammy Value"); 
    ... return some ActionForward. 
    } 
} 

在JSP:

<span>The value of the Blammy variable is this here thing: ${Blammy}</span> 

<span>The value of the Blammy variable is this here thing: <c:out value="${Blammy}"/></span> 

一旦你的基本概念下來,正好使用List設置請求屬性並訪問它在JSP中使用迭代器標籤。

+0

謝謝大家的幫助 – 2012-02-13 01:17:46