• 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

Dealing with a Vector<object> in JSP

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
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
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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    Bartender
    Posts: 1682
    7
    Android Mac OS X IntelliJ IDE Spring Linux
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    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
    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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    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
     
    Ranch Hand
    Posts: 146
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Marshal
    Posts: 28193
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic