Using findAncestorWithClass() is tempting, because it's simple and clean. However, it causes problems with included pages, because it doesn't work across page boundaries. Using page context has a similar problem, as Hal Deadman points out.
Prior to Struts 1.0, Struts used to use page context (I don't know if it ever used findAncestorWithClass()). However, that was changed to use request context, to avoid the issues described above. Particularly since this is going to be a property of the outermost (X)HTML tag, I would encourage the use of request scope, for maximum flexibility. Something else I would recommend is that the property tested by other tags (i.e. the request attribute) be a Boolean instead of a String. Then you can use Boolean.TRUE and Boolean.FALSE objects, compare them directly using '==', and avoid costly string conversions. Finally, I don't believe there's a need for an HtmlTei class. That should eliminate the source of the problem you're having. ;-) -- Martin Cooper At 01:57 PM 11/12/01, Craig R. McClanahan wrote: >On Sun, 11 Nov 2001, Matt Raible wrote: > > > Date: Sun, 11 Nov 2001 22:00:40 -0700 > > From: Matt Raible <[EMAIL PROTECTED]> > > Reply-To: Struts Developers List <[EMAIL PROTECTED]> > > To: [EMAIL PROTECTED] > > Subject: Converting struts-html tags to be XHTML-compliant > > > > I am taking on the project of converting (with backwards compatibility) all > > the struts-html tags to be XHTML-compliant. > > > >Matt, THANK YOU for taking this on. > >I've got a suggestion for a slightly different implementation approach. >It would go something like this: > >* Implement "xhtml" as a boolean property of the HtmlTag class. > >* In subordinate tags that need to know the current setting, > use the findAncestorWithClass() method of the TagSupport class > (which all the Struts tags implement) to walk back through the > tag hierarchy nesting to locate the surrounding <html:html> tag > (if there is one). Alternatively, you can walk the chain yourself > with getParent(). > >This is similar to the technique used by things like the <html:text> tag >to find their surrounding <html:form> tag, and avoids polluting the >attribute namespace for what is really a low-level presentation detail. >In typical use, I'd bet that it even has better performance than the >HashMap put and get that is required for page attributes. > >Craig > > > > I'd like to use this e-mail to tell you my approach, ask you for > advice, and > > get your feedback. > > > > 1. Approach: > > > > In HtmlTag.java, add > > if (xhtml) > > pageContext.setAttribute("xhtml", "true"); > > > > In all other html-producing tags (i.e. img, input), do a check > when closing > > the tag: > > > > // check if this is an XHTML document > > String xhtml = (String) > pageContext.getAttribute("xhtml"); > > > > if (xhtml != null && xhtml.equals("true")) { > > results.append("\" />"); // extra space > before close > > will allow XHTML to work in older browsers > > } else { > > results.append("\">"); > > } > > > > 2. Advice: > > > > Should I be setting the "xhtml" variable in the pageContext, request, or > > session. Page seems to make the most sense since that is what this > variable > > relates to. However, to do this (to my understanding), I have to create > > HtmlTei.java and add the <teiclass> declaration to the struts-html.tld: > > > > public class HtmlTei extends TagExtraInfo { > > > > /** > > * Return information about the scripting variables to be > created. > > */ > > public VariableInfo[] getVariableInfo(TagData data) { > > > > return new VariableInfo[] { > > new VariableInfo(data.getAttributeString("xhtml"), > > "java.lang.String", > > true, > > VariableInfo.NESTED) > > }; > > > > } > > > > } > > > > However, this does not work as I'd expect it to. What I'm expecting is > that > > I am exposing the "xhtml" variable simply by adding the HtmlTei class - but > > it's not working at all. > > > > 3. Feedback: > > > > Please let me know what you think of this approach. By doing this, and > > adding a check if the user wants the doc to be XHTML, we should achieve > full > > backwards compatibility, and be able to convert the struts-html tags to be > > XHTML-compliant. > > > > Thanks, > > > > Matt > > > > > > > > > > > > -- > > To unsubscribe, > e-mail: <mailto:[EMAIL PROTECTED]> > > For additional commands, e-mail: > <mailto:[EMAIL PROTECTED]> > > > > > > >-- >To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> >For additional commands, e-mail: <mailto:[EMAIL PROTECTED]> -- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>