velocity-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Bill Boland" <>
Subject RE: Velocity vmlibs
Date Fri, 17 May 2002 06:56:52 GMT
Tags are beans...but they do have several dependencies on other objects
in the JSP world (PageContext, JspWriter, etc.) and are poked and
prodded a bit by their JSP containers before the methods are called from
within the JSP pages. 

An example to illustrate this. The Struts <bean:size/> tag returns the
size of a Collection or Map (for more info, see
Within the JSP page it looks something like:

<bean:size id="itemListSize" name="itemList" />

When it is compiled by Tomcat 4 into java code, it turns into the
following snippet of code:

                /* ----  bean:size ---- */
_jspx_th_bean_size_0 = new org.apache.struts.taglib.bean.SizeTag();
                java.lang.Integer itemListSize = null;
                try {
                    int _jspx_eval_bean_size_0 =
                    itemListSize = (java.lang.Integer)
                    if (_jspx_eval_bean_size_0 ==
                        throw new JspTagException("Since tag handler
class org.apache.struts.taglib.bean.SizeTag does not implement BodyTag,
it can't return BodyTag.EVAL_BODY_TAG");
                    if (_jspx_eval_bean_size_0 !=
javax.servlet.jsp.tagext.Tag.SKIP_BODY) {
                        do {
                            itemListSize = (java.lang.Integer)
                        // end
                        // begin
                        } while (_jspx_th_bean_size_0.doAfterBody() ==
                        itemListSize = (java.lang.Integer)
                    if (_jspx_th_bean_size_0.doEndTag() ==
                } finally {

If this isn't enough to convince one to move to Velocity or use a better
strategy, I'm not sure what is. 

I find

   #set ( $itemListSize = $req.getAttribute( "itemList" ).size() )


   #set ( $itemList = $req.getAttribute( "itemList" ) )
   (using $itemList.size() when I need the size.)

much more desirable when working with Servlets than the tags. But, of
course, I'm preaching to the choir here.

And as for this SizeTag bean, it isn't as simple as just dropping it
into the context. Minimally, you might have to do the following on a
SizeBean to place it into context so it can be "used".

	SizeTag sizeTagBean = new SizeTag();
	sizeTagBean.setPageContext( pageContext );
	context.put( "size", sizeTagBean );

and this assumes you have a PageContext object. To use it in a template
it might like:

	#set ( $dummy = $size.setId( "itemListSize" ) )
	#set ( $dummy = $size.setName( "itemList" ) )
	#set ( $dummy = $size.doStartTag() )
	#set ( $itemListSize = $pageContext.getAttribute( "itemListSize"
) )

(There's isn't a public method on the tag bean to actually get the size.
The doStartTag() method begins the behavior of the tag bean after the
parameters have been set and placing results into page context
attributes or writing out to the response stream.)

Once it's used, it probably should not be reused since many tag beans
hold state and cannot be used as reusable tools on a page. That's why
most are created new each time they are used on a page so the default
settings for that tag are restored.

I don't mean to make fun of this bean, JSPs (well...) or how it might be
used. Its interface and purpose were not intended for the template
world. Many taglibs have not been designed as "tag adapters" that
separate the "taggy-ness" or JSP glue from the actual bean logic. And,
if they do, the underlying beans may not be as well documented as the
tags. But I have seen some that have been well designed and their
underlying bean libraries could be used. Usually, the beans already
existed and somebody wrote a tag adapter to them.

The intent here is to learn from all of this and not go down a path that
is well worn but goes into the dark woods. Velocity has some great
technological challenges as well...and user acceptance and education
being probably the toughest nut to crack.

Warner's original intent of this thread was to develop tool libraries
for use with Velocity that give newbies more desire to use it with these
neat prepackaged tools. Then standardize the tools to some degree so
that they can then foster collections of macro libraries that use these
standard tools and pages and applications that are build on Velocity. If
JSP taglibs tag beans can be adapted or their underlying code much the better. If not, we blaze a new trail out of the
dark woods. 

A VSTL (Velocity Standard Tool Library) would be a nice offering
(beginning with the tools in the velocity-tools already). I think we
should propose what tools we need developed or what we can offer to a
library and develop a plan. Maybe there is one and I'm just too green on
the scene! But, in this area is where I'm planning to contribute over
the next few weeks, if I may.


-----Original Message-----
From: Jon Scott Stevens [] 
Sent: Thursday, May 16, 2002 8:39 PM
To: velocity-dev
Subject: Re: Velocity vmlibs

on 5/16/02 7:10 PM, "Terry Steichen" <> wrote:

> Jon,
> I don't know enough about JSP to know exactly how to do what you're
> suggesting - but it sounds pretty interesting.  Would you mind
humoring me
> with couple more exmaples to help illustrate this (potentially
> approach?
> Regards,
> Terry

With Velocity, you put java.lang.Object's into the Context.

Given that a taglib is simply a java.lang.Object, you simply instantiate
and put it into the context. You can then call on its methods from
the Velocity template.

I'm not quite sure what else you are asking for. This is pretty simple


To unsubscribe, e-mail:
For additional commands, e-mail:

To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message