Craig McClanahan wrote:
On 11/17/05, David Whipple <[EMAIL PROTECTED]> wrote:


I would like to be able to create a custom tag which is able to use the
struts <html:text>, <html:dropdown> or <html:hidden> fields. I am not able
to find a way to create a tag library that can produce/call/reference
other
tags.

You didn't miss anything ... that strategy is not technically feasible.
There is only one pass through the compile stage that converts a JSP page
into Java code ... even if you wrote a custom tag that output "<html:text
...>" that text would just be literally written to the output stream.

There are a couple of ways to approach this, though; see below.

I am interested in making a custom tag for our application which will

either show a text entry field, a dropdown or a static text entry based on
the choices available to a particular user. The choices will probably be
passed in as a collection in the request, where a single value results in
a
hidden field (no choice for the user), a set of values results in a
dropdown, and a special case of a single value zero, which means the user
can enter anything they like through a text entry field.

You'd need to write a new custom tag that copies the rendering logic of the
tags you are choosing between, suitably conditioned with an if statement
that checks the condition.

Alternatively, you can write a custom tag which delegates to the appropriate Struts tag when called. Essentially, in your doStartTag / doEndTag methods (or doTag if using SimpleTag), you have to decide which Struts tag you want to use, then instantiate it and set it up as the container would -- calling it's setters, doStartTag, doEndTag, etc in the appropriate order.

It's a bit messy; the functionality the tags encapsulate isn't exposed in a way you can access directly. A cleaner alternative if you're using JSP 2.0 is to use a tag file, which can then just contain standard JSP to select and invoke the appropriate delegate tag. There are some issues with doing this currently, which I have a work-in-progress solution to (see http://issues.apache.org/bugzilla/show_bug.cgi?id=33064 for details).

I have searched everything I can think of and can't seem to find anything

that can allow this.

Does anyone know a way to approach this?

Thanks,
Dave

Craig

PS: This kind of need is an area where component architectures like
JavaServer Faces can be handy ... it's much easier in that world to create a
component that conditionally creates a child component of the correct type,
and then renders it, without having to duplicate all the rendering logic.

It's also a good argument for seperation of concerns ;-) It would be feasible to split the html tags into 'tag call protocol handling' and 'html rendering' class pairs, where the rendering classes could then easily be called from any custom tag without the burden of duplicating the custom tag calling invocation requirements.

Note that's a lot of work and change, which I don't see as likely to happen since this isn't a particularly common requirement. Just thought I'd point out that it's possible ;-)

L.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to