Custom JSP taglib. Provides a simple template based tool. Sure, templates are the almost mandatory part of any Java-based web development toolbox nowadays, so it is yet another. It is based on the approach we've used in many projects. As any of our components it is based on two main things: it must be simple and the overhead must be minimal. Template taglib has been build around <jsp:include> tag.
Custom tag include is similar to the basic <jsp:include>. You can use it
in two forms:
<%@ taglib uri="taglib.tld" prefix="t" %> <t:include page="your_page.jsp"/> it is similar to the basic <jsp:include>. In the second form
you can pass included page through some attribute (e.g. in request, session or application scope).
<%@ taglib uri="taglib.tld" prefix="t" %> <t:include attribute="attribute_name"/> this form lets you insert the file identified by the value of the specified attribute. Default scope for attribute
is request, but it could be session, application or page as well. So you can use this form in your templates.
You will be able to use the same layout over and over again simply by passing a different value for one or more of the attributes. For example your template
may include header, body and footer. E.g.:
File layout.jsp: <%@ taglib uri="taglib.tld" prefix="t" %>
Main file (uses layout): <%@ taglib uri="taglib.tld" prefix="t" %>
To use the same layout with a different body you can simply substitute the attributes. Tag setAttribute lets you define a value for the specified attribute. Also you can use this tag
in the body of include tag:
<t:include page="layout.jsp"> <t:setAttribute name="header" value="header.jsp"/> <t:setAttribute name="body" value="my_body_file.jsp"/> <t:setAttribute name="footer">footer.jsp</t:setAttribute> </t:include> in this case attributes will be removed after the rendering of this layout. Tag getAttributeValue lets you print a value for the specified attribute. You can pass
your own bean as a value for some attribute. In this case tag getAttributeProperty lets you call
the specified method. Tag executeAttribute lets you execute the chunk of JSP code
identified by the value of the specified attribute. E.g.:
File layout.jsp: <%@ taglib uri="taglib.tld" prefix="t" %>
Main file (uses layout): <%@ taglib uri="taglib.tld" prefix="t" %>
Here tag getAttributeValue will be executed only for the users in the specified role. The usage of getAttributeProperty here is an analogue for myBean.name() or myBean.getName() call. Also tag will ignore all possible errors. And tag executeAttribute executed the JSP code identified by the value of the specified attribute. Tags getAttributeValue and getAttributeProperty both are body tags. Body will be used as a default value if the given attribute (or property) does not exist. Tag setAttribute is actually a body tag also. So you can set a new value either as a parameter value or as a body for this tag. Tag escape lets you escape JSP (HTML) tags, so in our example tag's body will be passed to setAttribute just as a string. When ignoreErrors is set to true errors will not be reported (e.g. if the attribute is not present). When container-based authentication is being used, you can specify also a role (or commas separated list of allowed roles) for template tags. If user is not in the specified role than the appropriate tag will be skipped. For getAttributeValue (getAttributeProperty) tag you can save the obtained value in the page scope variable specified in the optional parameter id. Tag setAttribute is just a wrapper (with some add-ons) for pageContext.setAttribute() call. So you
can continue to use for example some like this for setting your attribute:
<% pageContext.setAttribute("header", "My value for header", pageContext.REQUEST_SCOPE); %> Also, for getting this attribute you can use this:
<%=pageContext.getAttribute("header", pageContext.REQUEST_SCOPE); %> Tags are: include Body tag, includes the given content (page). Parameters are: 1) page Optional parameter. Describes a JSP page to be included.
setAttribute Body tag, defines an attribute. Parameters are: 1) name Describes a name for attribute
escape Body tag, escapes HTML (JSP) codes. Parameters are: none getAttributeValue Body tag prints the value for the given attribute. Parameters are: 1) attribute Describes an attribute
getAttributeProperty Body tag prints the value for the given property. Parameters are: 1) attribute Describes an attribute
executeAttribute Executes the code identified by the value of the specified attribute. Parameters are: 1) attribute Describes an attribute
for downloading: Library: template.jar Description: taglib.tld See also Coldtags suite - the largest collection of custom JSP tags.
|
Also in Coldtags:
|