Pages

Wednesday, June 1, 2016

JSP Standard Tag Library (JSTL)


What is JSTL ?




It is a standard library of custom tags. In other words, it is a collection of useful
JSP tags which  can remove scriplet code from a JSP page.

     According to their functionality,  these tags are classified into 5 groups.They are

  •    Core Tags
  •    Formatting Tags
  •    SQL Tags
  •    XML Tags
  •    JSTL Functions
                                                   Before discussing on the above topics in detail, let us see how JSTL can be integrated with our existing project




How to integrate JSTL library in the project?


Maven


Step 1: Update pom
<dependency>
    <groupid>javax.servlet</groupid>
    <artifactid>jstl</artifactid>
    <version>1.2</version>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupid>taglibs</groupid>
    <artifactid>standard</artifactid>
    <version>1.1.2</version>
    <scope>runtime</scope>
</dependency>
Step 2: Update viewClass
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
    <property name="prefix">
        <value>/WEB-INF/jsp/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

Step 3: Use JSTL tags in .jsp file

<h1>Welcome message : <c:out value="${message}"></c:out></h1>


Eclipse


Step 1: Download JARs

Download jstl-1.2.jar (JSP 2.1 containers only i.e. Tomcat 6, otherwise jstl-1.1.jar) fromhttp://repo1.maven.org/maven2/javax/servlet/jstl/1.2/ 


Step 2: Copy JARs 

    • Copy to your project's WEB-INF/lib directory

Step 3: Use JSTL tags in .jsp file
    • Include the following tags in yours jsp's:
      • <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
      • <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
      • <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>



Different Groups in JSTL tag library

  • Core Tags



  • Formatting Tags



  • SQL Tags       =>  provides tags required to interact with relational databases


  • XML Tags  => provides ways to interact with XML data like parsing XML,  transforming                                 XML data etc.



  • JSTL Functions  => most are common string manipulation functions




Benefits of usibg JSTL ?

No comments:

Post a Comment