aspose file tools*
The moose likes JSP and the fly likes Using resource bundle as input for a custom tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Using resource bundle as input for a custom tag" Watch "Using resource bundle as input for a custom tag" New topic
Author

Using resource bundle as input for a custom tag

Jon Chase
Greenhorn

Joined: Feb 14, 2004
Posts: 7
I've written a custom tag that is invoked like this:
<custom:tag title="a title"/>
And I'm trying to accomplish the following in my JSP page:
<fmt:bundle base="messages">
<custom:tag title="<fmt:message key="foo.key"/>"/>
</fmt:bundle>
That is, I want to use a message from a resource bundle as input to my custom JSP tag.
Is this at all possible (hopefully in a straightfoward way)??
Thanks,
Jon
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60992
    
  65

You didn't mention whether this was under JSP 1.2 or JSP 2.0. If the latter,
something along the lines of the following might work (completely untested!):

Under JSP 1.2, perhaps:


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jon Chase
Greenhorn

Joined: Feb 14, 2004
Posts: 7
Excellent! Since the first way didn't work (I'm using tomcat 5.x, isn't that jsp 2.0?), I wound up using a hybrid of your ideas:

And just made my custom tag class look for a variable named "ia.title" in the pageContext.
Thanks so much!!
Jon
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60992
    
  65

I'm using tomcat 5.x, isn't that jsp 2.0?

Yes.

And just made my custom tag class look for a variable named "ia.title" in the pageContext.

Just be aware that that creates a strong coupling between your tag and its usage on the page, which could limit the reusability of the tag. While that's not always a bad thing, just be aware of it and make sure to use this type of technique only where appropriate.
Jon Chase
Greenhorn

Joined: Feb 14, 2004
Posts: 7
Good advice! To try to keep the coupling as low as possible, I've gone ahead and made the "title" attribute a non-required attribute in the TLD, so users can do either:

Thanks for your suggestions!
Jon
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using resource bundle as input for a custom tag