Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using resource bundle as input for a custom tag

 
Jon Chase
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64851
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Jon Chase
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64851
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic