Template taglib ver. 1.5

    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" %>

<t:include attribute="header"/>
<t:include attribute="body"/>
<t:include attribute="footer"/>

Main file (uses layout):

<%@ taglib uri="taglib.tld" prefix="t" %>

<!-- define attributes -->
<t:setAttribute name="header" value="header.jsp"/>
<t:setAttribute name="body" value="my_body_file.jsp"/>
<t:setAttribute name="footer">footer.jsp</t:setAttribute>

<!-- include (render) layout page -->
<t:include page="layout.jsp"/>

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" %>

<center><t:getAttributeValue attribute="header" role="admin"/></center>
<t:getAttributeProperty attribute="bean" property="name" ignoreErrors="true"/>
<t:executeAttribute attribute="code">

Main file (uses layout):

<%@ taglib uri="taglib.tld" prefix="t" %>

<!-- define attributes -->
<t:setAttribute name="header" value="My document"/>
<t:setAttribute name="bean" value="<%=new myBean()%>"/>
<t:setAttribute name="code">
 <t:escape>
  <% out.println("this code will be executed"); %>
 </t:escape>
</t:setAttribute>

<!-- include (render) layout page -->
<t:include page="layout.jsp"/>

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.
2) attribute Optional parameter. Describes an attribute. Value of this attribute will be treated as a JSP page to be included.
3) scope Optional parameter. Describes a scope for attribute. Possible values are page, request, session, application. Default value is request.
4) flush Optional parameter. Possible values are true, false. Default value is false.
5) role Optional parameter. Describes a commas separated list of allowed roles. By default is empty (tag will be executed regardless of role).
6) ignoreErrors Optional parameter. This parameter enables/disables error messages. Possible values are true, false. Default value is false (errors reporting is enabled).

setAttribute

Body tag, defines an attribute. Parameters are:

1) name Describes a name for attribute
2) value Optional parameter. Describes a value for attribute
3) scope Optional parameter. Describes a scope for attribute. Possible values are page, request, session, application. Default value is request.
4) role Optional parameter. Describes a commas separated list of allowed roles. By default is empty (tag will be executed regardless of role).
5) cond Optional parameter. Describes a boolean value tag's behavior depends on. Default value is true (set 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
2) scope Optional parameter. Describes a scope for attribute. Possible values are page, request, session, application. Default value is request.
3) id Optional parameter. Describes a name for the page scope variable. Without this parameter tag just prints data.
4) type Optional parameter. Describes a type for the page scope variable. Default value is java.lang.Object.
5) role Optional parameter. Describes a commas separated list of allowed roles. By default is empty (tag will be executed regardless of role).
6) ignoreErrors Optional parameter. This parameter enables/disables error messages. Possible values are true, false. Default value is false (errors reporting is enabled).

getAttributeProperty

Body tag prints the value for the given property. Parameters are:

1) attribute Describes an attribute
2) property Describes a property
3) scope Optional parameter. Describes a scope for attribute. Possible values are page, request, session, application. Default value is request.
4) id Optional parameter. Describes a name for the page scope variable. Without this parameter tag just prints data.
5) type Optional parameter. Describes a type for the page scope variable. Default value is java.lang.Object.
6) role Optional parameter. Describes a commas separated list of allowed roles. By default is empty (tag will be executed regardless of role).
7) ignoreErrors Optional parameter. This parameter enables/disables error messages. Possible values are true, false. Default value is false (errors reporting is enabled).

executeAttribute

Executes the code identified by the value of the specified attribute. Parameters are:

1) attribute Describes an attribute
2) scope Optional parameter. Describes a scope for attribute. Possible values are page, request, session, application. Default value is request.
3) role Optional parameter. Describes a commas separated list of allowed roles. By default is empty (tag will be executed regardless of role).
4) ignoreErrors Optional parameter. This parameter enables/disables error messages. Possible values are true, false. Default value is false (errors reporting is enabled).

for downloading:

Library: template.jar    Description: taglib.tld

© Coldbeans      Comments?

See also Coldtags suite - the largest collection of custom JSP tags.

Also in Coldtags: