动态行在jsp页面中创建

时间:2010-09-05 05:16:07

标签: java jsp struts

好吧......这是一个很长的问题。但我认为答案很简单。虽然我自己找不到解决方案。 t在jsp页面中连续有四列。我想在页面中使用循环添加10行,其中字段的名称类似于

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>

3 个答案:

答案 0 :(得分: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(开头很丑)。

答案 1 :(得分:1)

要在JSP页面中执行循环,可以使用JSTL <c:forEach>。 您需要下载JSTL的实现,请参阅下面的链接。


资源:

答案 2 :(得分:1)

在Struts logic taglib上,您可以使用 iterate 标记,如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 [0] .prop ,它将更新元素 0 的属性 prop >对于集合 mycollection ,如果提交了包含此逻辑的表单。

另请注意,Struts团队鼓励您仅使用struts标记,而不能使用Struts 1.x网站上所述的JSTL标记:

  

注意:这里有一些功能   taglib也可以在   JavaServer Pages标准标记库   (JSTL)。 Struts团队鼓励这样做   使用标准标签   尽可能使用Struts特定标签。