在iterate标签内部使用indexed属性
Using the Indexed Attribute in the Scope of the Iterate Tag
常常有这样一种需求,一个form中包含多个提交项,而项的数目是不固定,同时每个项还需要同用户进行交互。例如:一个关于培训反馈的调查表,对于不同的培训,调查的项目是不同的。而我们要做的是不论调查项目如何变化,我们的jsp,formbean和action则不用做任何修改。
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中…
3.Action中
ApplicationTrainingForm applicationTrainingForm = (ApplicationTrainingForm) form;
FeedBackItemBean[] pageItems = applicationTrainingForm.getPageItems();
即可获得用户输入的值。
本文的一切权利归本文作者所有,未经许可不得转载。