How to use JSTL with JSP and Struts with example code. Where to Download JSTL & Standard Jars

Generic tasks such as iteration, conditional processing, data formatting, internationalization, XML manipulation, and data access are made easy for JSP developers by Java Server Pages Standard Tag Library (JSTL ) which includes a variety of tags. Now JSP developers can make use of JSTL tags which is a good alternative to scriptlet and JSTL expression language (EL) which simplifies the access to the java language. Two jar files (jstl.jar and standard.jar) are required to make use of JSTL. Now let us download the jar files required to use the tags and EL.

There may be confusion where to download the two jar files (JSTL.jar and Standard.jar) which contains various JSTL tag handler classes and .tld files. I am providing the exact location to download the above two files.

You can download the Standard.jar file from the location http://repo2.maven.org/maven2/taglibs/standard/1.1.2/ and click on the file standard-1.1.2.jar to save into local system. You can download the jstl.jar file from the location http://repo2.maven.org/maven2/javax/servlet/jstl/1.2/ and click on the file jstl-1.2.jar to save into local system. jstl-1.2.jar and standard-1.1.2. are compatible with Java EE 5 / JSP 2.1.

To check your JSP, Java, servlet and server version which you are using , you can visit my earlier post JSP / Java / Server version
Old versions of jstl & standard jars can be download from http://search.maven.org/#browse%7C2056229056 and http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22taglibs%22%20AND%20a%3A%22standard%22 respectively

Now let us see how to use the JSTL tags in JSP either using struts or without using struts. Steps to follow

1. Download the two jar files from the above location
2. Put the two jar files into the Web application’s library directory (/WEB-INF/lib)
3. Make a Reference of the Tag Library Descriptors (TLD file) in the JSP.

Two ways are there to make reference between your JSP pages and the tag library

Method I) Make use of an absolute URI for core library which is used for iteration, conditional processing , etc .. in the JSP as given below

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

When you use value attribute to accept any expression like <c:out value=”${country}”/>, if you get the error according to TLD or attribute directive in tag file, attribute value does
not accept any expressions, you can use the following URIs instead of above URI.

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

OR

<%@ taglib uri=’http://java.sun.com/jstl/core_rt’ prefix=’c’%>

For more detail on why the above error occurs and how to solve, you can go through, Solution for attribute value does not accept any expressions

Similarly,

For XML library which is used for XML Processing, you can use the following absolute URI

<%@ taglib uri=”http://java.sun.com/jstl/xml” prefix=”x”%>
OR
<%@ taglib uri=”http://java.sun.com/jstl/xml_rt” prefix=”x”%>
OR
<%@ taglib uri=”http://java.sun.com/jsp/jstl/xml” prefix=”x”%>

 

For SQL library (prefix : sql) which is used for data access

<%@ taglib uri=”http://java.sun.com/jstl/xml” prefix=”sql”%>
OR
<%@ taglib uri=”http://java.sun.com/jstl/sql_rt” prefix=”sql”%>
OR
<%@ taglib uri=”http://java.sun.com/jsp/jstl/sql” prefix=”sql”%>

 

For format library (prefix : fmt) which helps for localization, the following URI is used in the JSP

<%@ taglib uri=”http://java.sun.com/jstl/fmt” prefix=”fmt”%>
OR
<%@ taglib uri=”http://java.sun.com/jstl/fmt_rt” prefix=”fmt”%>
OR
<%@ taglib uri=”http://java.sun.com/jsp/jstl/fmt” prefix=”fmt”%>

 

Method II)  Copy the necessary .tld files like c.tld, x.tld, sql.tld etc to the folder WEB-INF. You can get the .tld files in the folder META-INF when you extract the latest jar files

a) You can directly add a JSP declaration to any jsp page that needs to use the tag
<%@ taglib uri=”/WEB-INF/c.tld” prefix=”c”%>

OR

b) To make a static reference, add the following entry to the web.xml   file

<jsp-config>

<taglib>

        <taglib-uri>ctld</taglib-uri>

	<taglib-location>/WEB-INF/c.tld</taglib-location>

 </taglib>

</jsp-config> 

then you can add the following JSP declaration to any jsp page

<%@ taglib uri=”ctld” prefix=”c”%>

Note : Ist method is the preferred one.

Some of the struts tags are replaced with JSTL tags.

Sample use of JSTL tags

1. Example code using struts logic:iterate

<logic:iterate id="city" collection ="<%=cities%>">
Country : <%=city%>

</logic:iterate> 

the JSTL equivalent code for the struts logic:iterate

		        <c:forEach var="city" items="${cities}">
		            <c:out value="${city}"/>
		        </c:forEach> 

2. <bean:define id=”empcode” value =”employeeObj”/>

equivalent JSTL is

<c:set var =”empcode” value=”${employeeObj}”/>

Example JSP code using JSTL:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri='http://java.sun.com/jsp/jstl/core' prefix='c'%>
<html>
    <head>
       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
       <title>JSTL tag Example</title>
    </head>

 <body>

         <%
            String[] cities = new String[]{"Bangalore", "Chennai", "New Delhi", "Mumbai"};
            pageContext.setAttribute("cities", cities);
         %>   

	<table>
		<tr><td>JSTL Example</td>
		<tr><td>
		        <c:forEach var="cities" items="${cities}">
		            <c:out value="${cities}"/>
		        </c:forEach>
		</td>
		</tr>
	</table>   

 </body>
</html>

Reference : Using JSTL

You may also like

Leave a Reply

Be the First to Comment!