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
Joined: Mar 22, 2000
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
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