Posts Tagged ‘jsp

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



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

Join 77 other followers

September 2017
S M T W T F S
« Mar    
 12
3456789
10111213141516
17181920212223
24252627282930

Blog Stats

  • 830,680 hits