有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在jsp页面中创建java动态行

好的。。。这是一个很长的问题。但我认为答案很简单。虽然我自己找不到解决办法。在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) 个答案

  1. # 1 楼答案

    在struts logic taglib上,您可以使用Svtruts 1中记录的iterate标记。x站点:

    Repeat the nested body content of this tag over a specified collection

    您的代码将具有以下结构:

    <%@ 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>
    

    这将生成一个具有name属性的文本字段,如mycollection[0]。prop如果提交了包含此逻辑的表单,则将更新集合mycollection的元素0的属性prop

    还要注意,Struts团队鼓励您只使用Struts标记,而不能使用Struts 1中所述的JSTL标记。x站点:

    Note: Some of the features in this taglib are also available in the JavaServer Pages Standard Tag Library (JSTL). The Struts team encourages the use of the standard tags over the Struts specific tags when possible.

  2. # 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(一开始就很难看)