开发者

Losing the data while iterating the table by Rows with Java & JSP

开发者 https://www.devze.com 2023-03-03 02:25 出处:网络
In the jsp page, depending on the number of records, this tr will be executed and displayed to the users.

In the jsp page, depending on the number of records, this tr will be executed and displayed to the users.

for(int i=0; i<NoOfRecords.length;i++){
<tr>
    <td width="15%"> Transit Account & <%= acctId%>
    </td>
    <td width="15%"> <%=MultiModeConstants.GL_ACCT_NO%>
    </td>
    <td width="45%">
    <input type="text" id="multiModeAcctNo" name="multiModeAcctNo" desc="Multi Mode Transit Account Number" maxlength="9" class="body" size="9" tabindex="3" >
    </td>
    <td width="15%">
        Deposit
    </td>
    <td width="15%">
        $ <%= transactioAmount%>
    </td>
&l开发者_运维知识库t;/tr>
}

Based on the number of records, Number of rows would be populated in the jsp page, multiModeAcctNo, field will be entered by User(multiModeAcctNo);

For example if there are 4 records, there are 4 times I enter different multiModeAcctNo in the page.

After submitting the page, I am able to get only the first field, I am losing the other 3 values.

Suppose if I only read One Row, then I am able to get the data fine. What do I need to do?

I am using java & jsp as the programming languages.


You have to create dynamic names for input fields may be as:

for(int i=0; i<NoOfRecords.length;i++){
<tr>
    <td width="15%"> Transit Account & <%= acctId%>
    </td>
    <td width="15%"> <%=MultiModeConstants.GL_ACCT_NO%>
    </td>
    <td width="45%">
    <input type="text" id="multiModeAcctNo_<%=i%>" name="multiModeAcctNo_<%=i%>" desc="Multi Mode Transit Account Number" maxlength="9" class="body" size="9" tabindex="3" >
    </td>
    <td width="15%">
        Deposit
    </td>
    <td width="15%">
        $ <%= transactioAmount%>
    </td>
</tr>
}

Now in your servlet [hoping that you also get NoOfRecords.length there] get value of each input by providing it's dynamically generated names. May be as:

String str = new String[NoOfRecords.length];
for(int i=0; i<NoOfRecords.length;i++){
    str[i] = request.getParameter("multiModeAcctNo_"+i);
}


I'm not sure what kind of submission you're doing but you may want to use a different ID for your input field, as you iterate you are using the same multiModeAcctNo ID in your <input>.

If you're using a form and POSTing the element you may want to name your input to something like multiModeAcctNo[] then in your JSP file refer it as a POST array element (not sure if that's possible with Java). Just an idea.

0

精彩评论

暂无评论...
验证码 换一张
取 消