You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Scott Van Wart <sc...@indosoft.com> on 2006/08/30 19:48:24 UTC

Extending tag libraries

This is a little OT, but it, uh, kind of has to do with Struts :).

I'm interested in adding a few attributes to some of the struts tags.  
They're really only useful in the context of a specific web project I'm 
working on, so I'd really prefer not to edit the Struts TLD and 
implementation (for various other reasons too :).

Problem is, a lot of these have to do with view/layout, so I wanted to 
use .tag files.  Everything's fine if I extend a single property, say 
<html:text property="blah" />:

  <%@ attribute name="property" required="true" type="java.lang.String" %>

  <html:text property="${property}" />

Problem is, there are a lot more attributes, most are optional, and 
different permutations, etc.  Is there a relatively easy way of doing 
this in .tag files?  Do I have to use tag classes and, if so, is it 
possible to tell the JSP processor to re-compile the stuff I output so 
it doesn't write <html:text property... /> verbatim to the client?

- Scott

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Extending tag libraries

Posted by Laurie Harper <la...@holoweb.net>.
Scott Van Wart wrote:
> This is a little OT, but it, uh, kind of has to do with Struts :).
> 
> I'm interested in adding a few attributes to some of the struts tags.  
> They're really only useful in the context of a specific web project I'm 
> working on, so I'd really prefer not to edit the Struts TLD and 
> implementation (for various other reasons too :).
> 
> Problem is, a lot of these have to do with view/layout, so I wanted to 
> use .tag files.  Everything's fine if I extend a single property, say 
> <html:text property="blah" />:
> 
>  <%@ attribute name="property" required="true" type="java.lang.String" %>
> 
>  <html:text property="${property}" />
> 
> Problem is, there are a lot more attributes, most are optional, and 
> different permutations, etc.  Is there a relatively easy way of doing 
> this in .tag files?  Do I have to use tag classes and, if so, is it 
> possible to tell the JSP processor to re-compile the stuff I output so 
> it doesn't write <html:text property... /> verbatim to the client?

Unfortunately, this isn't really possible. Most tags behave differently 
if attributes are specified with an empty string / null value compared 
to if the attribute isn't provided. I brought this up, probably about a 
year ago now, and there was a fair bit of discussion. There's even a 
patch in bugzilla somewhere that implements one of the proposed 
solutions. Unfortunately, I never finished implementing the alternative 
solution so we could compare the two approaches and decide which (if 
either) should be rolled into Struts.

To answer your second question, no, you can't have a tag output 
<html:text ...> and have the JSP processed twice or something. You would 
need to write tags that generated the actual target markup. Again, 
extending the Struts tags this way isn't as easy as it could be. The 
basic pattern would be to sub-class the tag you want to extend, add the 
additional attributes you need, then make sure you do any attribute 
value manipulations in doStartTag() before calling super(). There are 
definitely limits to what you can achieve this way, but anything you 
might expect to be able to do with a .tag file should be achievable this 
way too.

L.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org