| Author |
Dealing with a Vector<object> in JSP
|
John Goodwin
Greenhorn
Joined: Sep 10, 2012
Posts: 3
|
|
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.
Servlet code
The JSP Code
I keep getting the following error;
"Cannot convert [com.Layer.Types.Product@5e876450] of type class java.util.Vector to class [Ljava.lang.Object;"
I could really use some ideas about what I'm doing wrong.
Thanks!
John
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56224
|
|
Welcome to the Ranch!
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.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
John Goodwin
Greenhorn
Joined: Sep 10, 2012
Posts: 3
|
|
Hey Bear,
First, thanks for your kind response.
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.
Thanks,
John
|
 |
Bill Gorder
Bartender
Joined: Mar 07, 2010
Posts: 1282
|
|
Umm. Why create a custom tag library offering only a subset of the functionality of that already provided with JSTL?
http://docs.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/c/forEach.html
|
[How To Ask Questions][Read before you PM me]
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56224
|
|
|
I agree with Bill. Save custom tags for things that the JSTL doesn't provide and use the JSTL for what it does.
|
 |
John Goodwin
Greenhorn
Joined: Sep 10, 2012
Posts: 3
|
|
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.
Thanks
John
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56224
|
|
|
 |
Luke Zechariah
Ranch Hand
Joined: Sep 27, 2005
Posts: 106
|
|
John, Bear, Bill,
How do you write the jsp if I have to write using custom tags only for the code in Tag Handler class below:
Thanks,
Luke.
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
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.
|
 |
 |
|
|
subject: Dealing with a Vector<object> in JSP
|
|
|