• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

dynamic value to retrieve textbox

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my textbox names are dynamic: tbox_0, tbox_1, etc. I want to then retrieve the values like:

<h3>The value in tbox</h3>
<ol>
<c:forEach var="loop" begin='0' end='${e.counter}' varStatus="lStat" >
<li><c ut value="${param.tbox_${lStat.count}}" /></li>
</c:forEach>

It does not like having ${lStat.count} inside of the ${param though. How can I use dynamic names to retrieve my tbox values?

[ May 16, 2006: Message edited by: Mr.David Shapiro ]


side question: if I set the name to my textbox to just tbox, it concates all entries done to tbox together. What delimitor can I use to split it. If I knew what the delim was, I could avoid the interpolation issue above.
[ May 16, 2006: Message edited by: Mr.David Shapiro ]
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mr.David Shapiro:

It does not like having ${lStat.count} inside of the ${param though.



Of course not. The ${} is not some sort of "fetch" operator -- it encloses the entire EL expression, which do not nest.

If you want to form dynamic parameter names, you can use <c:set> to create intermediate variables to hold the name. Then you can use that variable to index into the param hash.

For example:



A few things about your code:
1) Don't mix " and ' as attribute value delimiters. Pick one and be consistent.
2) Pick better variable names than lStat.
3) Use index rather than count on the status variable to get the current 0-based index.
4) When posting code, be sure to disable smilies so that your <c:out> tags look less surprised.
 
Mr.David Shapiro
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, that helped a lot. Why does index equal 1 for the first item even though begin is set to 0?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why does index equal 1 for the first item even though begin is set to 0?



Imagine that you loop through a collection this way :
begin = 5, end = 15, and step = 5
Count will be 1, 2, 3
But Index will be 5, 10, 15


Count actually 'counts' the number of items you have processed.
Index simply indicated the index of the collection.
 
Mr.David Shapiro
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand the count/index difference, but I expected when I used index that the numbers I would see if begin=0 is 0 1 2 3 4, etc. Instead, I got 1 2 3 4 5, etc with your code example.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Run the following test code:



Your results should be:


Test 1

n = 0
loop.index = 0
loop.count = 1
n = 1
loop.index = 1
loop.count = 2
n = 2
loop.index = 2
loop.count = 3
n = 3
loop.index = 3
loop.count = 4
n = 4
loop.index = 4
loop.count = 5
n = 5
loop.index = 5
loop.count = 6

Test 2

n = 10
loop.index = 10
loop.count = 1
n = 15
loop.index = 15
loop.count = 2
n = 20
loop.index = 20
loop.count = 3
n = 25
loop.index = 25
loop.count = 4



Also, remember that the javadoc is your friend.
 
reply
    Bookmark Topic Watch Topic
  • New Topic