You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Zia, Asad" <As...@ps.net> on 2001/03/02 19:08:01 UTC

Struts tags in custom tags/includes

I am unable to get Struts tags (html, bean etc.) to work in includes or
custom tags and can only use explicit html. In my custom tags, I want to
simply write to the page context out:

out.print("<html:img page=\"/images/cat.gif\" width=\"27\" height=\"30\"
border=\"0\"/>");

Instead, I have to do this: 
out.print("<img src=\"images/cat.gif\" width=\"27\" height=\"30\"
border=\"0\">");

I do not understand why the tags work in the JSP itself but not in includes
or tags. Why does the include/tag try to resolve its contents instead of
just outputting to the JSP and the JSP do the resolving? The taglib URIs are
listed in the JSP page. Solutions will be appreciated.


Asad Zia



Re: Struts tags in custom tags/includes

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
"Zia, Asad" wrote:

> I am unable to get Struts tags (html, bean etc.) to work in includes or
> custom tags and can only use explicit html. In my custom tags, I want to
> simply write to the page context out:
>
> out.print("<html:img page=\"/images/cat.gif\" width=\"27\" height=\"30\"
> border=\"0\"/>");
>
> Instead, I have to do this:
> out.print("<img src=\"images/cat.gif\" width=\"27\" height=\"30\"
> border=\"0\">");
>
> I do not understand why the tags work in the JSP itself but not in includes
> or tags. Why does the include/tag try to resolve its contents instead of
> just outputting to the JSP and the JSP do the resolving? The taglib URIs are

> listed in the JSP page. Solutions will be appreciated.
>

Support for JSP custom tags (not just Struts tags) exists *only* in a JSP page,
which is compiled into a servlet the first time it is used.  You are not allowed
to dynamically create JSP source text (as your example above does) and then
expect it to be compiled.

To use this tag, you would need to do so in a "real" JSP page that includes:

    <html:img page="/images/cat.gif" width="27" height="30" border="0"/>

If the information you are trying to render is dynamic, you can use scriptlet
expressions to do this on the fly.  For example, let's assume you have a MyImage
class to represent the fields of your image:

    public class MyImage {
        public String getPage();
        public int getWidth();
        public int getHeight();
        public int getBorder();
        ... other methods as needed
    }

and you have stored an instance of this bean (say, loaded from a database) under
key "image".  You can render a dynamically configured image like this:

    <jsp:useBean id="image" class="com.mycompany.MyImage"
     scope="request"/>

    <html:img page="<%= image.getPage() %>" width="<% image.getWidth() %>"
     height="<%= image.getHeight() %>" border="<%= image.getBorder() %>" />


> Asad Zia

Craig McClanahan