• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dealing with a Vector<object> in JSP

 
John Goodwin
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

  •  
    John Goodwin
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 1682
    7
    Android IntelliJ IDE Linux Mac OS X Spring
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 64959
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 64959
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
     
    Luke Zechariah
    Ranch Hand
    Posts: 128
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Sheriff
    Posts: 21117
    32
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic