File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Dealing with a Vector<object> in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Dealing with a Vector<object> in JSP" Watch "Dealing with a Vector<object> in JSP" New topic
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: 60046
    
  65

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.


  • [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by 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: 1632
        
        7

    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: 60046
        
      65

    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: 60046
        
      65

    Luke Zechariah
    Ranch Hand

    Joined: Sep 27, 2005
    Posts: 117
    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: 18115
        
        8

    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Dealing with a Vector<object> in JSP
     
    Similar Threads
    calling java files from jsp page
    classpath problem
    Packages
    n-dimensioned array in Java
    Displaying prices