• 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

Embedding Custom Tag in Java Code

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic