• 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

String[] as attribute to custom tag

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible to pass a String[] as an attribute to a custom tag? I have looked through the JSP specification and see nothing prohibiting this. I have written my setters & getters appropriately, but do not see a way to actually pass the array to the tag. Something like this:

Unfortunately, all the examples I have encountered deal with primitives or Strings, not objects.
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
-------
Unfortunately, all the examples I have encountered deal with primitives or Strings, not objects.
-------
Well, a Stringis an Object a String[] isnt -- its an array
 
James Childers
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're correct, of course. The original question remains, though: Is it possible to pass non-primitives/non-Strings as parameters to a custom tag?
 
sharp shooter, and author
Posts: 1913
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, in the TLD file, simply set the rtexprvalue of your tag to true. Then, write the setter method in the tag handler to accept a String[] parameter. Finally, in your JSP, just use a request-time expression to pass in the value. For example:

Hope that helps
Simon
 
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. I've been dabbling a little with this. I haven't been able to get the setter method in a custom tag to read an array parameter short of using a runtime expression. Is this the only way to pass an array to a custom tag?..it just seems kinda odd since jstl is a means to avoid scripting inside jsp or whatever...

I'm employing a javabean to return an arraylist consisting of both String objects and String array objects. I used the standard jstl library (i.e. c:forEach) to parse the String array elements from the returned arraylist. Everything was great, however, I wanted to experiment see if I could do this with the arrays passed to a custom tag. I keep getting an "illegal start of expression" exception on the tag setter method....

setQ8list(String [] q8)

I think what is happening is that it's not reading the array as an array but as an object and the setter sees the object as a String...

[Ljava.lang.String;@1a7c484

I found I am able to pass collection objects to tags ok, but arrays seem to be different and require runtime scripting on the jsp. Is this accurate? Thank you very much for reading this.
[ November 01, 2005: Message edited by: Tom Griffith ]
 
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 found I am able to pass collection objects to tags ok, but arrays seem to be different and require runtime scripting on the jsp. Is this accurate?



If by that you mean scriptlet expression, then no, it is not accurate.

I keep getting an "illegal start of expression" exception on the tag setter method....



That sounds like a compile time issue. Need more info. Does this happen when compiling the bean? When translating the JSP? When running the JSP?
[ November 01, 2005: Message edited by: Bear Bibeault ]
 
Tom Griffith
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It happens when running the JSP. I converted the original jsp (which loads a javabean that returns an arraylist of string and string array objects) from scriptlets to EL. When an array object was encountered inside the arraylist, I used standard jstl to parse and display the individual elements, in this case Strings, from inside the array. For example...

<c:forEach var="listElement8" items="${Fsurvey_result_bean.result[76]}">
${listElement6}
<br><br>
</c:forEach>

The object reference ${Fsurvey_result_bean.result[76]} is a String array.

So I thought I'd mess with this a little and try to build a custom tag that would handle the iterating and printing out on the jsp of each element inside the String array...

<q8_prefix:q8_name q8List="${Fsurvey_result_bean.result[76]}" />

...and the tag class uses this setter...

private String[] q8List;

public void setQ8List( String [] q8List ) {
this.q8List=q8List;
}

the problem I encounter is that I keep getting "illegal start of expression" exceptions on the setter method argument. Like I said, I suspect the setter is getting passed something like [Ljava.lang.String;@9e75f6 instead of the actual array. When I get the exception, it places the problem at the [ in the setter argument.

Fsurvey_result_el2.jsp:640: illegal start of expression
_jsp_simpleq8_0.setQ8List(([Ljava.lang.String;_caucho_expr_77.evalObject...

In the past, I've been able to pass arraylists and vectors to custom tag handlers without this kinda problem...I jsut can't get this to work right with this array. I've tried casting this every which way. Thank you again...
[ November 01, 2005: Message edited by: Tom Griffith ]
 
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
If set up correctly, there should be no issues doing what you are trying to do. Arrays are not special in any way and can be passed as custom action attributes just like any other object.

Be sure that the q8List attribute definition in the TLD specifies that run-time expressions are allowed (EL expressions are still run-time expressions).

By the way, you never said, but I am assuming that this is within a JSP 2.0 environment, right?
 
Tom Griffith
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bear, yeah, the rtexprvalue tag is set to true for the q8List attribute.

However, I am suddenly given pause by that question regarding JSP 2.0. Let me google that and get back to you. Should I be downloading something that I don't know about?...all the development in j2ee 1.4 if that has anything to do with it. Oh man...
[ November 01, 2005: Message edited by: Tom Griffith ]
 
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
What are you using as your servlet/JSP container? Tomcat 5? If so, you are using JSP 2.0.

Also, if the EL is working for you outside the bounds of the JSTL, you're under JSP 2.0.

With regards to your issue: it's not the "array-ness" that's tripping you up. I don't know what is causing your issues, but it's probably time to test the various moving parts one by one. For example, test your tag by passing in a simple array that you constuct on-the-fly. Test that the entries in your structures are really what you think they are. And so on.
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Is it possible to pass a String[] as an attribute to a custom tag?



In jSP 2.0 you can. Not possible on earlier versions.

An alternative is to put the array in pagecontext and take it while inside the custom tag.
 
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

In jSP 2.0 you can. Not possible on earlier versions.



Incorrect. But obviously pre-JSP 2.0 you needed to use scriptlet expressions rather than the EL.
 
jiju ka
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please specify
Are you saying arrays can be passed in pre-JSP 2.0? If so how? I like to use it.

In pre-JSP2.0 I tried to pass String[]; The tld was not complaining about String[]. But at runtime array was found not-passed. Does it have something to do with set-up? Do we need to specify String[] anywhere else than tld.
[ November 01, 2005: Message edited by: jiju ka ]
 
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

Do we need to specify String[] anywhere else than tld.



It's not required in the TLD at all. The mutator for the attribute in the tag class must accept a String[], the attribute in the TLD must be declared to accept run-time expressions, and a scriptlet expression must be used to 'pass' the array to the tag.
[ November 01, 2005: Message edited by: Bear Bibeault ]
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>>Do we need to specify String[] anywhere else than tld.

>It's not specified in the TLD at all.
>The mutator for the attribute in the tag class must accept a String[

I thought you had to specify the type of the attribute as part of the tld.
It defaults to java.lang.String if you don't specify it.

In this case wouldn't it be:


I think I'll go play with this a bit...
 
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 actually forgot that you can specify an attribute type in the TLD. I never have, and I've never run into any trouble. Stefan, let us know if you find out anything interesting. I no longer have a JSP 1.2 setup for testing.
[ November 01, 2005: Message edited by: Bear Bibeault ]
 
Tom Griffith
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. I read up a little on the JSP 2.0 spec last night and didn't realize that only classic tags were supported pre-2.0. I've previously used simple tags and passed String objects and collections to respective tag handlers extending SimpleTagSupport so I guess I have to be running JSP 2.0. I'm going to try entering the type into the tld and see what happens...
 
Tom Griffith
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welp, it didn't like java.lang.String[] in the tld. The container is looking for a java.lang.String[] class and can't find anything. I started messing around with placing the array in the either request or page context yesterday like you suggest jiju so that's looking like the only alternative using EL. Just as a test, I changed the tag handler class to accept a String argument and passed it one of the String objects from the arraylist returned by the javabean and it took it ok. I then changed the tag handler setter to accept the entire arraylist returned from the bean and coded doTag() to process it and that went ok too. It still just appears to me that something is going on with tags reading arrays literally as a String and not String[]...at least on this jvm...thank you very much for the input or whatever so far everybody...
[ November 02, 2005: Message edited by: Tom Griffith ]
 
Stefan Evans
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I did some testing and found out you guys were right.
You don't need to specify the type of an attribute. It works fine without it.

The testing code:
tld:


java:


The Jsp page:


I defined two tags in the tld.
One which defined the attribute type, one which didn't.
Both worked successfully.
I tried this code on both Tomcat 4.1.30 and Tomcat 5.0.28.
I also modifed the tld to the latest xml schema, and tried it with a JSP2.0 SimpleTag. That also worked.

The question is why then would we bother to specify the type in the tld file? Maybe it is to automatically produce documentation from the tld (the equivalent of javadoc) Thats about the only reason I can think of.

One hurdle I did discover is that the tags used in the tld changed between versions of the DTD. Mainly with annoying hyphens.
For instance <tag-class> and <tagclass>, <jsp-version> and <jspversion>
Makes migrating tlds between versions of DTDs a big bundle of pain.

Well, I learned something ;-)
Cheers,
evnafets
 
Tom Griffith
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stefan, like you, I've been able to you use scriplets and expressions to initialize and send an array reference to a tag handler. What I am stumbling on is using an EL expression that evaluates to an array as the tag setter argument...ie...

<q8_prefix:q8_name q8List="${Fsurvey_result_bean.result[76]}" />

where "${Fsurvey_result_bean.result[76]}" references an array of Strings stored inside an arraylist returned by the javabean at index 76.

It just refuses to take it and instead reads the argument as a literal String, i.e. [Ljava.lang.String;@9e75f6. I suspect this doesn't come up often because of standard jstl's ability to iterate but I just can't get this to take. Any workaround seems to take the "scriptless" out of "scriptless jsp", whether it's pageContext attribute setting or obviously scriplets. This only happens with arrays, none of the collections or individual String objects I send to handlers have given me problems. Well, thank you again everybody...I'll continue to mess with it
 
Stefan Evans
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All I can really contribute now, is that it works for me.
Modifying my example to use EL (with a whole bunch of messy scriptlet code to simulate your example)
I have a bean, returning a List of String[] objects.
Accessing them with EL works fine.

Next thing to check would be: Is your datastructure actually what you think it is?

Oh, and there is a bunch of "useful info" found at the top of this JSP, which tells you what server you are running, and what servlet version it supports. I find it useful for troubleshooting sometimes.



Cheers,
evnafets
 
Tom Griffith
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi everybody. I think I figured this out and wanted to follow-up for future reference or whatever. The web.xml was referencing jsp version 2.3 in <web-app> (i.e. the version when I first built this web app with scriplets and junk). I changed it to 2.4 and now all jstl works ok. Also, I was deploying the web app on resin 3 which has out of the box jstl so apparently this explains the mixed results I was receiving (with web.xml referencing version 2.3). Only when I deployed the war on jboss 4 and lost all EL in the web app (that was interesting) was I able to pinpoint the problem to web.xml. Now with web.xml referencing version 2.4, the app works consistently in both containers. Thank you very much again.
[ November 07, 2005: Message edited by: Tom Griffith ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic