Showing posts with label JSTL with JSP. Show all posts
Showing posts with label JSTL with JSP. Show all posts

use JSTL with JSP and Struts with example code

Generic tasks such as iteration, conditional processing, data  formatting, internationalization, XML manipulation, and data access are made easy for JSP developers by  JavaServer 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  scriptlets 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 earier 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
  i) Make use of  an absolute URI for core library which is used for iteration , condtional 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"%>

  and 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"%>

 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

  <taglib>
  <taglib-uri>ctld</taglib-uri>
  <taglib-location>/WEB-INF/c.tld</taglib-location>
  </taglib>
         then you can add the following JSP declaration to any jsp page   <%@ taglib uri="ctld" prefix="c"%> 

Ist method is the preferred one.

Some of the struts  tags are replaced with JSTL tags . You can use struts tags only  when there is no equivalent JSTL tags as  .
 Sample use of JSTL tags

 1.  Example code using struts logic:iterate


  <logic:iterate id="country" collection ="<%=countries%>">
  Country : <%=country%><br> 
  </logic:iterate>  

  the JSTL equivalent code  for the struts  logic:iterate


   <c:forEach var="country" items="${countries}">
  <LI><c:out value="${country}"/>   </c:forEach> 

 2. <bean:define id="empcode" value ="employeeObj"/>
  
 equivalent JSTL is   <c:set var ="empcode" value="${employeeObj}"/>