I'm fairly new to JSP and I'm trying to send a Vector of Product objects to my forEach tag so I can create a table of each product. Each row will contain the Product members.
I use the forEach extensively and with great success on ArrayLists of strings and even vectors of strings and there seems to be no end of examples of using forEach with a single dimension string array but I need to do it on an vector of objects containing members that I'll be accessing via EL.
John Goodwin wrote:I'm fairly new to JSP and I'm trying to send a Vector of Product objects to my forEach tag
Use a List implementation rather than a Vector. Firstly, Vector is an old collection from the the Jurassic period of Java and shouldn't be used except in special circumstances, of which this is not one. ALso, Vector isn't supported by JSTL; List is.
The JSP Code
A few things about your code:
Ditch the imports. They're superfluous.
For the JSTL core library use the convential "c" prefix, not "taglib". Not following conventions is a faux pas.
The references to Taglib are refering to my custom tag library. I just like the custom tags. It works for all string lists pretty flawlessly and since I didn't want the tag to access the members, I figured it would be fine as Object.
I'll try changing it over from a Vector to an ArrayList.
I agree with Bill. Save custom tags for things that the JSTL doesn't provide and use the JSTL for what it does.
Joined: Sep 10, 2012
Honestly, I used the custom tags because it's just what I spent more time focusing on and what comes to mind first.
But you make a good point so I'll spend more time reaquainting myself with JSTL again.
Well, that tag library declaration says that after
the JSP compiler is expecting something which can be cast to an array of Objects. A Vector can't be cast to an array of objects.
You could change the signature of the method to something like
but then you're locked into using Vector and only Vector -- an extremely reactionary step.
However if you're dead set upon rewriting the JSTL tag, and you want to support all manner of Lists and arrays, then the signature you want is
along with various code which tests whether that Object is indeed a List or an array along with casts to the appropriate type before working with the object. But frankly I wouldn't bother when JSTL already does all of that for me.