tags:

views:

54

answers:

3

ok... its a long question. but i think the answer is simple. though i can't find the solution myself. t have the four columns in a row in a jsp page. i would like to add 10 more rows using a loop in the page where the fields will have name like

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

and so on.

more clearly

property="cib_borrower_report.loanType" would be in all ten rows in the form.

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

now if i want to do this naming using loop how to do this? how can i add 1,2,3.. in property???

if i can do this dynamically, it will help me in type of fetching the values. so please help.

<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>
A: 

In JSP <foreach/> tag you can get the index using varStatus attribute and add it to the property name.

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

I would suggest using a list instead of named property names (looks better and extends the dynamic approach). With the list you still need to loop over the output, but will have a much cleaner JSP (which is ugly to begin with).

Bivas
i understand your answer. but i am new to jsp. and it would be helpful if you give me an example
riyana
A: 

To do a loop in a JSP page, you can use the JSTL <c:forEach>. You'll need to download an implementation of the JSTL, see links below.


Resources :

Colin Hebert
A: 

On struts logic taglib you can use the iterate tag as is documented on Svtruts 1.x site:

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

You will have the following structure on your code:

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

For the kind interaction you need you can access your properties by their index as following:

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

This will generate a text field with the name attribute like mycollection[0].prop that will update the property prop of the element 0 for the collection mycollection if the form containing this logic is submitted.

Also notice that the Struts team encourages you to only use struts tags where you can't use JSTL ones as stated on Struts 1.x site:

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.

mtrovo