First, we need need the jstl.jar, so make an entry for it in your ivy.xml
<ivy-module version="2.0">
<info organisation="org.confucius" module="helloworld"/>
<dependencies>
<dependency org="javax.servlet" name="servlet-api" rev="2.5"/>
<dependency org="javax.servlet" name="jsp-api" rev="2.0"/>
<dependency org="jstl" name="jstl" rev="1.2"/>
<dependency org="log4j" name="log4j" rev="1.2.16"/>
</dependencies>
</ivy-module>
Run the Ant 'resolve' target to download the jstl.jar to your /lib directory.
Next, we should include jstl.jar in Eclipse classpath.
In Eclipse, go to Project->Properties->Java Build Path->Add Jars
and add the jstl.jar
We are now ready to use JSTL.
Type (or copy-paste) the following in your HelloWorld.jsp:
<html>
<head>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
</head>
<body>
<table>
<c:forTokens var="poet" delims="," items="Anna, Brian, Cathy, Dan, Erica">
<tr>
<td>${poet}</td>
</tr>
</c:forTokens>
</table>
</body>
</html>
In this, we have used JSTL "forTokens" custom tag to loop through a sequence of names. We then output the names in a HTML table.
If you build and deploy HelloWorld.jar, then browse to:
http://localhost:8080/HelloWorld/jsp/HelloWorld.jsp
You will see the list of names show up.
In this example, the names have been hard coded as a comma separated string. Realistically, the could have come from a SQL query.
No comments:
Post a Comment