Using the Java Standard Tag Library
Needed file structure for a project
To start a simple JSTL project, you will need the following file structure:
*.jsp
WEB-INF/web.xml
WEB-INF/lib/jstl.jar
WEB-INF/lib/standard.jar
... all other JAR files from the distribution
WEB-INF/tlds/struts.tld
WEB-INF/tlds/c.tld
WEB-INF/tlds/c-rt.tld
... all other TLD files from the distribution
This structure contains some files under WEB-INF which comes directly from the JSTL distribution.
You may just copy them into the appropriate location and start coding your app.
The location of these files is given in the web.xml file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<!-- The Welcome File List -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- JSTL TLD Descriptor -->
<taglib>
<taglib-uri>http://java.sun.com/jstl/core</taglib-uri>
<taglib-location>/WEB-INF/tlds/c.tldd</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jstl/xml</taglib-uri>
<taglib-location>/WEB-INF/tlds/x.tld</taglib-location>
</taglib>
<!-- more JSTL TLDs if they fit into your app -->
</web-app>
|
After you have installed (copied) and configured JSTL as explained, you can start to write your
JSPs, an example page follows:
<%@ page language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<html>
<body>
<c:import url="body.jsp">
<a href="<c:url value="bla.jsp"><c:param name="id" value="24"/></c:url>...</a>
<c:forEach items="${requestScope.result}" var="item">
Item: <c:out value="${item.name}"/>
</c:forEach>
<c:if test="${param.buy}">
buy something
</c:if>
<c:choose>
<c:when test="${param.id}">
id was given, ok.
</c:when>
<c:otherwise>
no id was given!
</c:otherwise>
</c:choose>
</body>
</html>
|
Where to get JSTL
You can get the Jakarta implementation of the JSTL spec, named the standard taglib,
here. Just
unpack the archives you download and copy the files into the appropriate directories, as
explained above.
back to toolbox page
|