Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to update List of bean items using JSTL from JSP?

Posted on 2007-10-08
8
Medium Priority
?
455 Views
Last Modified: 2013-11-24
Hi,

My usage of bean - seems a little strange and I don't know how to collect the values from JSP.

Here is what I have:

I have a variable type java.util.List

fmslist - it has two items in it
1. a date string
2. a List containing my bean DecDiv values.

Now, in a jsp, I showed these values in textboxes...
and now once the user updates certain fields of this DevDiv bean type values ...


I want to be able to collect all the values and udpate the db ..

How do I collect them??


Here are some snippet from the jsp:

<c:forEach var="ddupdatelistItem" items="${ddupdatelist}" varStatus="status">

<c:when test="${viewAllIndicator != 'Y'}">
<td width="20%"><c:out value="${ddupdatelistItem[1].fundId}"/></td>
<td align="center" width="20%"><c:out value="${ddupdatelistItem[0]}"/></td>
<td align="center" width="20%"><INPUT TYPE="TEXT" CLASS="tabletextfield_numeric" NAME="divRate" MAXLENGTH="16" SIZE="30.5%" VALUE="<c:out value="${ddupdatelistItem[1].divRate}"/>"></td>
<td align="center" width="20%"><INPUT TYPE="TEXT" CLASS="tabletextfield_numeric" NAME="shortTermCapitalGainRate" MAXLENGTH="16" SIZE="30.5%" VALUE="<fmt:formatNumber type="number" maxFractionDigits="8" groupingUsed="false" value="${ddupdatelistItem[1].shortTermCapitalGainRate}" />"></td>
<td align="center" width="20"><INPUT TYPE="TEXT" CLASS="tabletextfield_numeric" NAME="longTermCapitalGainRate" MAXLENGTH="16" SIZE="30.5%" VALUE="<c:out value="${ddupdatelistItem[1].longTermCapitalGainRate}"/>"></td>
</c:when>

</c:forEach>


Thanks.
_Esam.
0
Comment
Question by:_Esam
  • 4
  • 2
7 Comments
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 20036189
well there are two ways to do it..

since all your text boxes are having same names..
you can go a request.getParameters("shortTermCapitolGainRate"); it will return you a array of string..

similarly you can get all the values.. and then run a loop with update statement in your servlet/jsp.

another way is to have a hidden field ( name is ismodified).. now with use of javascript you can change the value of ismodified when user changes the values.
in your servlet you can selectively update the records where ismodified is having a changed value.
0
 

Author Comment

by:_Esam
ID: 20036552
The first option does make sense to me but at the same time sounds scary to me !!!

Because -

Say I have the list in JSP as:

FundId DivRate ShortTermsRate LongTermRate

1       1.0      .012            .014
2      1.4      .011            .013
3      1.6      .016            .015

Now,
In the servlet I can go ahead the get all the individual parameter values from the String[]..
looping through it...

But...the scary part is:

I might have to do it like:



for(int i=0;i<fundId.length;i++){  //fundId.length is request.getParameterValues("fundId")

      int fundId = (new Integer(request.getParameterValues("fundId")[i])).intValue());

      double divRate = (new Double(request.getParameterValues("divRate")[i])).doubleValue());
      double shortTermsRate = (new Double(request.getParameterValues("shortTermCapitalGainRate")[i])).doubleValue());
      double longTermRate= (new Double(request.getParameterValues("longTermCapitalGainRate")[i])).doubleValue());

}

Now, my concern is that all these 4 values, for a particular index [i], is a record for a table row

The way I am accessing them, I must have to guarantee that the index values will not be messed up for a particular row during the http request/respose???


That's why I was wondering if I could put these 4 values in an object and then put these objects in a List ???

Is it possible???


THanks.
_Esam.
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 20037111
>>The way I am accessing them, I must have to guarantee that the index values will not be messed up for a particular row during the http request/respose???

This is a valid concern. Though it always comes in order but there is no documentation to guarntee it.

another way is to use javascript and keep a hidden variable where you store these values as comma separted list and use it to update the data base... ( along with ismodified  variable).
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:_Esam
ID: 20041516
>>another way is to use javascript and keep a hidden variable where
you store these values as comma separted list and use it to update
the data base... ( along with ismodified  variable).


I would greatly appreciate if you could illustrate how I could do it with the javascript...
I am not too great with js.


Thansk much...
_Esam..
0
 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 1000 total points
ID: 20044585
in your table you can create two hidden fields in each row.

& use a base name as ismodified & data

<td align="center" width="20%"><INPUT TYPE="hidden"  NAME="ismodified<c:out value="${index}"/> "></td>
<td align="center" width="20%"><INPUT TYPE="hidden"  NAME="datafields<c:out value="${index}"/> "></td>
 
put a javascript function on the fields
<td align="center" width="20"><INPUT TYPE="TEXT" CLASS="tabletextfield_numeric" NAME="longTermCapitalGainRate" MAXLENGTH="16" SIZE="30.5%" VALUE="<c:out value="${ddupdatelistItem[1].longTermCapitalGainRate}"/>" onKeyUp="changed(this.parent.parent);"></td>

now make the javascript function

function changed(row)
{
row[4].value='true'; // here it is assumed that the hiddenfield ismodified is 5th element of TD
row[3].value = row[0].value+','+row[1].value... so on..

}
now in servlet. just loops from ismodified0 to ismodifiedX.. and whenever you find a "TRUE";
use the datafieldX variable to get the data and update your database..

the code that I have put in is not tested/free of syntax errors... so use it accordingly/
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 20044604
another way is to append the index in every field that you have and then you do not have to use the DataField..
I think it is a better and more robust solution.
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20439017
Forced accept.

Computer101
EE Admin
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month20 days, 23 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question