2010-09-05 112 views
2

好的......它的一个长问题。但我认为答案很简单。尽管我自己找不到解决方案。 t在jsp页面中有四行。我想在页面中添加10多行使用循环的字段将有名称像在jsp页面中创建动态行

row1_amount, row1_loantype,row1_date, row1_status 
row2_amount, row2_loantype,row2_date, row2_status 

等等。

更清楚

property="cib_borrower_report.loanType"将在表单中的所有十行。

property="cib_borrower_report.loanType1" 
property="cib_borrower_report.loanType2" 
property="cib_borrower_report.loanType3" 

现在,如果我想使用循环做这个命名如何做到这一点?我如何添加1,2,3 ..属性?

如果我可以动态地做到这一点,它将帮助我获取值的类型。所以请帮助。

<table border="0" cellpadding="1"><tbody> 
    <tr> 
     <td ><label class="desc"><bean:message key="label.cib.new.report.taken.amount"/></label></td> 
     <td><html:text property="cib_borrower_report.takenAmount" styleClass="SingleLineTextField" size="20"></html:text></td> 
     <td>&nbsp;&nbsp;</td> 

     <td><label class="desc"><bean:message key="label.cib.new.report.loan.type"/></label></td> 
     <td><html:text property="cib_borrower_report.loanType" styleClass="SingleLineTextField" size="20"></html:text></td> 
     <td>&nbsp;&nbsp;</td> 

     <td><label for="cib_borrower_report.reportingDate" class="desc"><bean:message key="label.cib.new.reporting.date" /></label></td> 
     <td> 
      <table><tbody><tr> 
        <td><input type="Text" name="cib_borrower_report.reportingDate" id="cib_borrower_report.reportingDate" style="cib_borrower_report.reportingDate" class="SingleLineTextField" maxlength="10" size="10" tabindex="1" ></td> 

       <td><a href="javascript:NewCal('cib_borrower_report.reportingDate','mmddyyyy')"><img align="middle" src="Images/cal.jpg" width="20" height="20" border="0" alt="Pick a date"></a></td> 
      </tr></tbody></table> 
     </td> 
     <td>&nbsp;&nbsp;</td> 

     <td><label class="desc"><bean:message key="label.cib.new.loan.status"/></label></td> 
     <td align="center"> 
      <html:select property="cib_borrower_report.loanStatus" styleId="searchQuery1"> 
       <html:option value="STD">STD</html:option> 
       <html:option value="SMA">SMA</html:option> 
       <html:option value="SS">SS</html:option> 
       <html:option value="DF">DF</html:option> 
       <html:option value="BL">BL</html:option> 
      </html:select> 
     </td> 
    </tr> 
</tbody></table> 

回答

2

在JSP <foreach/>标签,你可以使用varStatus属性的景气指数,并将其添加到属性名称。

 
<c:forEach var="bean" items="${item}" varStatus="status"> 
    Item: <c:out value="${item}"/> 
    Item Index: <c:out value="${status.index}"/> <!-- Starts from zero --> 
    Item Count: <c:out value="${status.count}"/> <!-- Starts from one --> 
</c:forEach> 

我会建议使用列表而不是命名的属性名称(看起来更好,并扩展了动态方法)。有了这个列表,你仍然需要循环输出,但是会有一个更干净的JSP(这很难看)。

+0

我明白你的答案。但我是jsp的新手。如果你给我一个例子,这将是有益的 – riyana 2010-09-05 07:30:52

1

Struts的标签库的逻辑,你可以使用迭代标签作为上Svtruts 1.x的现场记录:

重复此标签的嵌套主体内容 在指定 集合

你会对你的代码结构如下:

<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> 
<table><tbody> 
<logic:iterate id="formName" name="mycollection"> 
    <tr> 
     <!-- CONTENT OF EACH ROW --> 
    </tr> 
</logic:iterate> 
</tbody></table> 

对于你需要,你可以通过它们的索引来访问你的财产的那种互动如下:

<logic:iterate id="formName" name="mycollection" indexId="idx"> 
    <html:text name="formName" property='<%= "mycollection[" + idx + "].prop" />' /> 
</logic:iterate> 

这将产生具有类似MyCollection的name属性的文本字段[0]。道具将更新道具的属性的元素收集mycollection如果包含此逻辑的表单被提交。

还要注意的是,Struts的团队鼓励你只使用Struts标签,你不能使用JSTL那些Struts的1.x的站点说明:

注:一些功能在这 的taglib也可在 JavaServer Pages标准标签库 (JSTL)中获得。 Struts团队鼓励 在可能的情况下通过 Struts特定标签使用标准标签。