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

Embedding Custom Tag in Java Code

 
John Zimmerman
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JSP page that uses custom tags and I want to pass the value of the custom tag to a bean. It doesn't seem possible and I wondered if anyone has a solution to this problem. For discussion-sake, assume I have a custom tag called ZipCode which returns the current zip code of the user. Assume also that I have a bean which looks up the city name based on zip code. So the code in the JSP would be
<% String cityName = bean.getCityName(<tag:ZipCode/> ); %>
Of course this doesn't work because the java code gets compiled BEFORE the custom tag is resolved. Any ideas or workarounds on how to do this?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your custom tag could attach some value to the pageContext object with setAttribute() and later code could retrieve the value. As I recall, your custom tag automatically has a pageContext.
Bill

------------------
author of:
 
chris leonardi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John,
You might look into using creating a TagExtraInfo class that allows you to set a scripting variable from the ZipCode tag extension that's accessible to your page (and ultimately, your bean, I believe).
I like William's idea, too - I use that technique a lot with tag extensions, as it affords some flexibility in using and manipulating the tag extension's 'return value'. In fact, the Jakarta Struts framework employs this approach for enabling communication between beans and tag extensions.
Hope that helps,
Chris
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic