• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

JSTL & JavaScript

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I'm trying to populate a JavaScript array variable with a specific data variable from a List of bean (a class that contains data variables with getters and setters--I think that's called a bean) objects passed in on the request. For instance, I have a List of bean objects, each with a firstName and lastName variable (with getters and setters for each variable). From that List, I want to populate a JavaScript array variable with as many first names as there are bean objects (each which contain a firstName variable) in the List. Would anyone know of a clever way of doing so using JSTL? Is it even possible? Any help would be greatly appreciated.

lugos
 
Sheriff
Posts: 67754
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
Seems pretty straight-forward to me; no cleverness required. You'd iterate over the list like any other using <c:forEach> and then use template text and EL expressions to build up the Javascript.

Why not give it a go and post what you come up? We can help you out with any foibles you might encounter.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as the server knows, Javascript is just text -- no different from HTML or any other text.

Just loop through your bean and write out the JS elements to the page.


[you beat me, this time]
[ July 27, 2005: Message edited by: Ben Souther ]
 
Samuel Lugo
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I was able to get it to work...

// This line is inside another function and the variable is declared elsewhere.
listOfNames = populateArray();

function populateArray() {
<c:forEach items="${list}" var="listOfNames">
names[i] = "<c ut value='${listOfNames.name}' escapeXml='false' />";
i = i + 1;
</c:forEach>
return names;
}

I had thought of something like this before, but I didn't think "i" would be incremented in a JSTL loop (I'm fairly new to JSTL). I thought I would have to have it in a JavaScript loop. I suppose using a JSTL tag like <c:set var="i" value="${0}" /> and then incrementing it with <c:set var="i" value="${i + 1}" /> could also work. I haven't tried it though.

Thanks for the help,
lugos
 
Bear Bibeault
Sheriff
Posts: 67754
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
It would be instructive for you to do a View Source to see how your question about incrementing i is resolved.

Also, you could clean up your Javascript a bit (removing the need to increment i) by using the push() method of the Javascript array, or, since this is all being set up on server before sending the page to the browser, by using a Javascript array initializer.
[ July 27, 2005: Message edited by: Bear Bibeault ]
 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Keep your java code and jstl out of js.

[ July 28, 2005: Message edited by: Garrett Smith ]
 
Bear Bibeault
Sheriff
Posts: 67754
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
Since the OP doesn't have the string of names as a comma-separated list, but rather already in a collection, this solution isn't very applicable.

Using the split method of the JS String could be somewhat clever in other situations however (though I'd still prefer doing the processing on the server rather than deferring it to client-side mechanisms).
[ July 28, 2005: Message edited by: Bear Bibeault ]
 
Garrett Smith
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then replace the "[" and "]" with "".


"[apple, pear, love, salad]". replace(/^\[|\]$/g, "");

"apple, pear, love, salad".split(",");


It's kind of messy and harder to manage when the server side code has js mixed in it. I've had the misfortune of dealing with mixed code on more than a few projects.

It's cleaner and simpler to output only strings to javascript and deal with them there.
[ July 29, 2005: Message edited by: Garrett Smith ]
 
Ranch Hand
Posts: 41
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Samuel Lugo wrote:
I had thought of something like this before, but I didn't think "i" would be incremented in a JSTL loop (I'm fairly new to JSTL). I thought I would have to have it in a JavaScript loop. I suppose using a JSTL tag like <c:set var="i" value="${0}" /> and then incrementing it with <c:set var="i" value="${i + 1}" /> could also work. I haven't tried it though.



Sorry for bringing this up, but can anyone confirm if the bold content above would work? I tried that inside a nested:iterate and am getting


error. Any idea why?
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doing it explicitly, you can make use of the forEach loops status variable to get a current count.



alternatively you could do

names[names.length] = "${oneName.name}";

And as has already been pointed out, you can intialise an Array using the constructor passing in all values, which is slightly less verbose.
Something like this:


The gotcha on this second syntax, is that it will fail if there is only one item in the list, as it assumes the method is the one that specifies how many elements should be in the javascript array.

 
Bear Bibeault
Sheriff
Posts: 67754
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
joseph prabhu,
Your post was moved to a new topic.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic