28
Jan
10

Using Tag Files with JSP 2.1 Servlet 2.5


This page is about using Tag files to create custom tags to replace the functionality provided by Tiles2.

Background

Tiles2 framework allow developers to define the layout of the web application in one place. It reduces the amount of duplicate code that is typically found for left, top navigation bars and footers. However in some cases Tile2 can be overkill.

Instead, simple layouts can be accomplished by using jsp tag files.

Requirements

  • Maven – installed and configured

Start a new Maven Project

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp

For the group id enter: com.test
For the artifactId enter: tagTestWeb

Answer the rest of the questions using defaults [Hit Enter].

cd to the project’s folder

Make your pom.xml file look like the one below. (replace it with this one if its easier for you)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.test</groupId>
  <artifactId>tagTestWeb</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>tagTestWeb Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.1.2</version>
</dependency>
<dependency>
    <groupId>taglibs</groupId>
    <artifactId>standard</artifactId>
    <version>1.1.2</version>
</dependency>
    <dependency>
  <groupId>org.apache.tiles</groupId>
  <artifactId>tiles-jsp</artifactId>
  <version>2.1.4</version>
</dependency>
  </dependencies>
  <build>
    <finalName>tagTestWeb</finalName>
<plugins>
	<plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-compiler-plugin</artifactId>
		<version>2.0.2</version>
		<configuration>
			<source>1.6</source>
			<target>1.6</target>
		</configuration>
	</plugin>

	<plugin>
		<groupId>org.mortbay.jetty</groupId>
		<artifactId>jetty-maven-plugin</artifactId>
				<version>7.0.0.v20091005</version>
		<configuration>
			<scanIntervalSeconds>2</scanIntervalSeconds>
		</configuration>
	</plugin>
</plugins>
  </build>
</project>

In the JSP 2.1 and Servlet 2.5 environment you can simply create custom tags by put the tag file into the tags folder under WEB-INF.

Create the directory that will hold the tag files.
mkdir src/main/webapp/WEB-INF/tags

Below we have created a simple tag that prints out a table with a title and some contents in the body.
vi src/main/webapp/WEB-INF/tags/box.tag

<%@ attribute name="color" required="true" rtexprvalue="false" %>
<%@ attribute name="title" required="true" rtexprvalue="false" %>
<table width="320" bordercolor="${color}" border="1" cellpadding="4" cellspacing="0">
	<tr bgcolor="${color}" color="#ffffff">
		<td class="boxHeader" nowrap>
			${title}
		</td>
	</tr>
	<tr>
		<td valign="top" class="boxText">
			<jsp:doBody/>
		</td>
	</tr>
</table>

The code below is another tag that will be included in the first tag. This will demonstrate the capabilities of including on tag inside the body of another.

src/main/webapp/WEB-INF/tags/subbox.tag

<%@ attribute name="color" required="true" rtexprvalue="false" %>
<%@ attribute name="title" required="true" rtexprvalue="false" %>
<table width="320" bordercolor="${color}" border="1" cellpadding="4" cellspacing="0">
	<tr bgcolor="${color}" color="#ffffff">
		<td class="boxHeader" nowrap>
			${title}
		</td>
	</tr>
	<tr>
		<td valign="top" class="boxText">
			<jsp:doBody/>
		</td>
	</tr>
</table>

Put the following into a jsp

src/main/webapp/index.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>
<html>
<body>
<h2>Hello World!</h2>
<tags:box>
	<jsp:attribute name="color">#314289</jsp:attribute>
	<jsp:attribute name="title">Testing 1234 Title</jsp:attribute>
	<jsp:body>
		This is the body of the text.
	</jsp:body>
</tags:box>
<br/>
<tags:box color="#314289" title="output from c:out tag">
	<jsp:body>
		<c:out value="output from c:out tag"/>
	</jsp:body>
</tags:box>
<br/>
<tags:box color="#314289" title="output from c:out tag">
	<jsp:body>
		<tags:subbox color="#314289" title="this is a sub box">
			<jsp:body>
				<c:out value="this is a sub box"/>
			</jsp:body>
		</tags:subbox>
	</jsp:body>
</tags:box>
</body>
</html>

Run the application

cd to the project base folder.
Run the following at the command prompt.

mvn jetty:run

Navigate to the url: http://localhost:8080/index.jsp

Including multiple sections (JSP Fragments)

Please see the comment from Michael Breed below about including multiple JSP fragments using tag files.

Advertisements

2 Responses to “Using Tag Files with JSP 2.1 Servlet 2.5”


  1. 1 Michael Breed
    November 9, 2010 at 1:23 am

    I know this is almost a year old now, but as I happen to be looking into using tag files myself, I thought I would correct the stated limitation here. It is in fact possible to inject multiple sections using what JSP terms “fragments” and using the jsp:invoke tag. Here’s how:

    Tag File = /src/main/webapp/WEB-INF/tags/page.tag

    <%@ attribute name="header" fragment="true" %>
    <%@ attribute name="footer" fragment="true" %>
    <html>
    <body>
        <jsp:invoke fragment="header" />
        <jsp:doBody />
        <jsp:invoke fragment="footer" />
    </body>
    </html>
    

    Main JSP file = /src/main/webapp/index2.jsp

    <%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>
    <tags:page>
        <jsp:attribute name="header">
            <ul>
                <li>Home</li>
                <li>About</li>
                <li>Contact</li>
            </ul>
        </jsp:attribute>
        <jsp:attribute name="footer">
            Copyright &copy; 2010 Michael Breed
        </jsp:attribute>
        <jsp:body>
            <h1>Welcome!</h1>
            <div>Hello World!</div>
        </jsp:body>
    </tags:page>
    
  2. November 9, 2010 at 8:11 pm

    Thanks for your input Michael! I have updated the page to include it.


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 77 other followers

January 2010
S M T W T F S
« Dec   Feb »
 12
3456789
10111213141516
17181920212223
24252627282930
31  

Blog Stats

  • 846,580 hits

%d bloggers like this: