我在JSP中使用<s:iterator>
標籤來顯示List
人物對象。如何在JSP中顯示動作數據?
我嘗試在動作類中創建ListOfPersons
作爲List
以及getter和setter。我仍然無法顯示數據 - 我該怎麼做?
<s:iterator value="ListOfpersons" status="stat">
當我嘗試打印列表的大小時,我得到零。
我在JSP中使用<s:iterator>
標籤來顯示List
人物對象。如何在JSP中顯示動作數據?
我嘗試在動作類中創建ListOfPersons
作爲List
以及getter和setter。我仍然無法顯示數據 - 我該怎麼做?
<s:iterator value="ListOfpersons" status="stat">
當我嘗試打印列表的大小時,我得到零。
如果我理解你的話,你在使用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>
我相信,如果List對象不是Map,那麼可以很容易地在JSP中引用List對象。因爲Object將被放置在堆棧的頂部,並且您可以像'firstName'等那樣輕鬆地引用它。 – 2012-02-09 04:59:10
@ UmeshAwasthi您能否給我示例? – batbaatar 2012-02-09 05:24:08
請參閱文檔中的示例。 http://www.struts.apache.org/2.3.1/docs/iterator.html – 2012-02-09 05:42:11
答案很簡單:存儲在JSP的請求,並訪問其中的信息。
再回應:
一些代碼(支柱的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中使用迭代器標籤。
謝謝大家的幫助 – 2012-02-13 01:17:46
我還不確定你在說哪個標籤?是一個自定義標籤還是它的'',請張貼行動代碼類以及標籤的詳細信息。 –
2012-02-09 03:32:34