Labels

.NET Job Questions About Java Absract class Abstract class Abstract Class and Interface Aggregation ajax aop apache ofbiz Apache ofbiz tutrial Association authentication autocad basics batch Binary Tree bootstrap loader in java build Builder design pattern C++ Job Questions caching CallableStatement in java certifications Chain of responsibility Design pattern charts check parentheses in a string Classes classloader in java classloading concept code quality collage level java program Composition concurrency Concurrency Tutorial Converting InputStream to String Core Java core java concept core java interview questions Core Java Interview Questions Core Java Questions core java tutorial CyclicBarrier in Java data structures database Database Job Questions datetime in c# DB Db2 SQL Replication deserialization in java Design Patterns designpatterns Downloads dtd Eclipse ejb example/sample code exception handling in core java file handling injava File I/O vs Memory-Mapped Filter first program in spring flex Garbage Collection Generics concept in java grails groovy and grails Guice Heap hibernate Hibernate Interview Questions how-to IBM DB2 IBM DB2 Tutorial ide immutable Interceptor Interface interview Interview Questions for Advanced JAVA investment bank j2ee java JAVA Code Examples Java 7 java changes java class loading JAVA Classes and Objects Java Classloader concept Java classloading concept java cloning concept java collection Java collection interview questions Java Collections java concurrency Java CountDownLatch java definiton Java design pattern Java EE 5 Java EE 6 Java Exceptions Java file Java Garbage Collection Java generics Java Glossary java hot concept java immutable concept Java Interface Java interview Question java interview question 2012 java interview question answer Java Interview Questions Java Interview Questions and Answers java interview topic java investment bank Java Job Questions java multithreading java multithreading concept java new features Java Packages java proxy object java questions Java Serialization Java serialization concept java serialization interview question java session concept java string Java Swings Questions java synchronization java threading Java Threads Questions java tutorial java util; java collections; java questions java volatile java volatile interview question Java Wrapper Classes java.java1.5 java.lang.ClassCastException JavaNotes javascript JAX-WS jdbc JDBC JDBC Database connection jdk 1.5 features JDK 1.5 new features Concurrent HashMap JMS interview question JMS tutorial job JSESSIONID concept JSESSIONID interview Question JSF jsp JSP Interview Question JSP taglib JSTL with JSP Junit Junit Concept Junit interview question.Best Practices to write JUnit test cases in Java JVM Linux - Unix tutorial Marker Interfaces MD5 encryption and decryption messaging MNC software java interview question musix NCR java interview question Networking Job Questions news Object Serialization Objects ojdbc14.jar OOP Oracle Oracle SQL Query for two timestamp difference orm own JavaScript function call in Apache ofbiz Packages Palm Apps patterns pdf persistence Portal Portlet Spring Integration Prime number test in java programs Rails Reboot remote computers REST Ruby Sample application schema SCJP security Senior java developer interviews servlet3 servlets session tracking singleton design pattern Spring Spring 2.5 Framework spring ebook Spring framework concept spring MVC spring pdf Spring Security Spring Security interview questions SQL SQL performance SQL Query to create xml file Sql Query tuning ssis and ssrs StAX and XML string concept string immutable string in java strings struts Struts2 Struts2 integration synchronization works in java Technical Interview testing tips Tomcat top Tutorial Volatile in deep Volatile working concept web Web Developer Job Questions web services weblogic Weblogic Application Server websphere what is JSESSIONID xml XML parsing in java XML with Java xslt


Saturday, 6 July 2013

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}"/>

No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...