1. 程式人生 > >在iterate標籤內部使用indexed屬性

在iterate標籤內部使用indexed屬性

iterate標籤內部使用indexed屬性<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Using the Indexed Attribute in the Scope of the Iterate Tag

常常有這樣一種需求,一個form中包含多個提交項,而項的數目是不固定,同時每個項還需要同用戶進行互動。例如:一個關於培訓反饋的調查表,對於不同的培訓,調查的專案是不同的。而我們要做的是不論調查專案如何變化,我們的jsp,formbeanaction則不用做任何修改。

1

.首先FormBean

 private FeedBackItemBean[] feedBackItems;

public void setPageItems(int index, FeedBackItemBean feedBackItem)

{

pageItems[index]=feedBackItem;

}

public FeedBackItemBean getPageItems(int index)

{

return pageItems[index];

}

/**

* @return Returns the pageItems.

*/

public FeedBackItemBean[] getPageItems()

{

return pageItems;

}

/**

* @param pageItems The pageItems to set.

*/

public void setPageItems(FeedBackItemBean[] pageItems)

{

this.pageItems = pageItems;

}

*請注意一下上面的get/set方法。

其中FeedBackItemBean 是使用者定義的Bean,為舉例,只列舉部分屬性:

private String feedBackItemTitle = null;

private String feedBackItemContent = null;

private long feedBackItemValue = 0;

2.在jsp

程式碼片斷如下:

<logic:notEmpty name="applicationTrainingForm" property="feedBackBean.feedBackItems">

<logic:iterate id="pageItems" name="applicationTrainingForm" property="feedBackBean.feedBackItems" indexId="nIndex">

<logic:equal name="pageItems" property="feedBackItemType" value="<%=String.valueOf(FeedBackConstants.FEEDBACK_CHOOSE)%>">

<TR>

<TD class="TableHeader"><bean:write name="pageItems" property="feedBackItemTitle"/></TD>

<TD width="35%" class="TableHeader">

<html:radio name="pageItems" indexed="true" property="feedBackItemValue" value="10">

10</html:radio>

<html:radio name="pageItems" indexed="true" property="feedBackItemValue" value="9">

9</html:radio>

<html:radio name="pageItems" indexed="true" property="feedBackItemValue" value="8">

8</html:radio>

<html:radio name="pageItems" indexed="true" property="feedBackItemValue" value="7">

7</html:radio>

<html:radio name="pageItems" indexed="true" property="feedBackItemValue" value="6">

6</html:radio></TD>

<TD width="35%" class="TableHeader"><html:radio name="pageItems" indexed="true" property="feedBackItemValue" value="5">

5</html:radio>

<html:radio name="pageItems" indexed="true" property="feedBackItemValue" value="4">

4</html:radio>

<html:radio name="pageItems" indexed="true" property="feedBackItemValue" value="3">

3</html:radio>

<html:radio name="pageItems" indexed="true" property="feedBackItemValue" value="2">

2</html:radio>

<html:radio name="pageItems" indexed="true" property="feedBackItemValue" value="1">

1</html:radio></TD>

</TR>

</logic:equal>

<logic:equal name="pageItems" property="feedBackItemType" value="<%=String.valueOf(FeedBackConstants.FEEDBACK_TEXT)%>">

<TR>

<TD class="TableHeader"><bean:write name="pageItems" property="feedBackItemTitle"/></TD>

<TD colspan="2" class="TableHeader"><html:textarea name="pageItems" indexed="true" property="feedBackItemContent" styleClass="TextBox" cols="100%"/></TD>

</TR>

</logic:equal>

</logic:iterate>

</logic:notEmpty>

注意,經解析以後的html程式碼如下

<TR>

<TD class="TableHeader">Structure, focus and logic.</TD>

<TD width="35%" class="TableHeader">

<input type="radio" name="pageItems[0].feedBackItemValue" value="10">10

<input type="radio" name="pageItems[0].feedBackItemValue" value="9">9

<input type="radio" name="pageItems[0].feedBackItemValue" value="8">8

<input type="radio" name="pageItems[0].feedBackItemValue" value="7">7

<input type="radio" name="pageItems[0].feedBackItemValue" value="6">6</TD>

<TD width="35%" class="TableHeader"><input type="radio" name="pageItems[0].feedBackItemValue" value="5">5

<input type="radio" name="pageItems[0].feedBackItemValue" value="4">4

<input type="radio" name="pageItems[0].feedBackItemValue" value="3">3

<input type="radio" name="pageItems[0].feedBackItemValue" value="2">2

<input type="radio" name="pageItems[0].feedBackItemValue" value="1">1</TD>

</TR>

<TR>

<TD class="TableHeader">Depth and breadth</TD>

<TD width="35%" class="TableHeader">

<input type="radio" name="pageItems[1].feedBackItemValue" value="10">10

<input type="radio" name="pageItems[1].feedBackItemValue" value="9">9

<input type="radio" name="pageItems[1].feedBackItemValue" value="8">8

<input type="radio" name="pageItems[1].feedBackItemValue" value="7">7

<input type="radio" name="pageItems[1].feedBackItemValue" value="6">6</TD>

<TD width="35%" class="TableHeader"><input type="radio" name="pageItems[1].feedBackItemValue" value="5">5

<input type="radio" name="pageItems[1].feedBackItemValue" value="4">4

<input type="radio" name="pageItems[1].feedBackItemValue" value="3">3

<input type="radio" name="pageItems[1].feedBackItemValue" value="2">2

<input type="radio" name="pageItems[1].feedBackItemValue" value="1">1</TD>

</TR>

<TR>

<TD class="TableHeader">Impact of activities</TD>

<TD width="35%" class="TableHeader">

<input type="radio" name="pageItems[2].feedBackItemValue" value="10">10

<input type="radio" name="pageItems[2].feedBackItemValue" value="9">9

<input type="radio" name="pageItems[2].feedBackItemValue" value="8">8

<input type="radio" name="pageItems[2].feedBackItemValue" value="7">7

<input type="radio" name="pageItems[2].feedBackItemValue" value="6">6</TD>

<TD width="35%" class="TableHeader"><input type="radio" name="pageItems[2].feedBackItemValue" value="5">5

<input type="radio" name="pageItems[2].feedBackItemValue" value="4">4

<input type="radio" name="pageItems[2].feedBackItemValue" value="3">3

<input type="radio" name="pageItems[2].feedBackItemValue" value="2">2

<input type="radio" name="pageItems[2].feedBackItemValue" value="1">1</TD>

</TR>

大家已經注意到了name的值:

pageItems[0].feedBackItemValue為例做一下說明,首先struts是以reflect機制工作的,於是他呼叫的方法應該為getPageItems(0).getFeedBackItemValue(),並以此方法來獲取值。讀到這裡,你應該明白了。

於是,剩下的工作就很簡單了,只需要在Action

3Action

ApplicationTrainingForm applicationTrainingForm = (ApplicationTrainingForm) form;

FeedBackItemBean[] pageItems = applicationTrainingForm.getPageItems();

即可獲得使用者輸入的值。

本文的一切權利歸本文作者所有,未經許可不得轉載。