Win a copy of Spark in Action this week in the Open Source Projects forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Using resource bundle as input for a custom tag

 
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
 
Marshal
Posts: 67414
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
Marshal
Posts: 67414
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
 
I wish to win the lottery. I wish for a lovely piece of pie. And I wish for a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic