tomcat-taglibs-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <Craig.McClana...@eng.sun.com>
Subject Re: Tag to replace <% String counter = "counter" %> in JSP...???
Date Tue, 25 Jul 2000 16:29:30 GMT
"Nuttall, Chris" wrote:

> Hi,
>
> Is it possible for a custom tag to declare/initialise
> variables within the tag handler class, to replace
> <% String counter = "counter" %> from within a
> JSP page?
>
> Perhaps...
>
> import javax.servlet.jsp.*;
> import javax.servlet.jsp.tagext.*;
>
> /**
>  * SetCounterTag adds String variable to page scope.
>  */
> public class SetCounterTag extends TagSupport {
>
>     public int doStartTag() throws JspException {
>
>         // String to be added
>         String counter = "counter";
>
>         pageContext.setAttribute("counter", counter);
>
>         // Continue processing this page
>         return (EVAL_PAGE);
>     }
> }
>
> ...which could be included within the JSP as:
>
> <tag:setCounter />
>
> ...and then access counter later in the JSP page, or
> from within another custom tag...???
>

Yes, this is possible, as long as you include a "TagExtraInfo" class
along with your Tag class.  TagExtraInfo describes (to the compiler) the
name of the scripting variable you are creating, and when it becomes
visible to the rest of the page (at the beginning of this tag, at the
end of this tag, or only from within the body of this tag).

You also have to do the pageContext.setAttribute() call, as you
described above, to store the value.  All the "TEI" class does is tell
the compiler what the variable name is going to be.

>
> Regards,
>
> Chris

Craig McClanahan



Mime
View raw message