JSTL 1.2.x guide

JSTL 1.2.x is the latest version of JSTL. There are a few differences between this version and the previous JSTL 1.1.x version. The most important difference is that JSTL 1.2.x supports Unified EL (Expression Language) , where as JSTL 1.1.x supports only traditional EL. With Unified EL it becomes very easy to combine the EL in JSF (Java Server Faces) and the EL in JSTL. This guide shows you how to install JSTL 1.2.x properly or troubleshoot your existing JSTL installation.

  • Download JSTL 1.2.x

    Get JSTL 1.2 from java.net Maven Repository for JSTL
  • Install JSTL 1.2.x

    Move the jstl-1.2.jar file to your project's WEB-INF/lib folder.
  • Configure JSTL 1.2.x

    This version of JSTL does not require any further configuration. See compatibility and depedency for more information.
  • JSTL 1.2.x Compatibility

    In short JSTL 1.2 is compatible with Unified EL, JSF 1.2, JSP 2.1, Servlet 2.5.

    JSTL 1.2.x requires a Web Server (Container) that complies with the Servlet 2.5 / JSP 2.1 (or higher) specification.

    For example: Tomcat 6.x complies with Servlet 2.5 and JSP 2.1 specifications.

    See: Tomcat Version - Servlet Spec - JSP Spec chart

    This also means that the deployment descriptor (web.xml file under your project's WEB-INF) folder must also comply with the Servlet 2.5 specification. This means that the web-app declartion and all the tags inside web.xml must be according to the Servlet 2.5 specification.

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        version="2.5">
    
    </web-app>
    Having a deployment descriptor and Web Container that is Servlet Specification 2.5 and JSP Specification 2.1 compliant means that the web container supports Unified EL which is used by JSTL 1.2.

    Return to Top

  • JSTL 1.2.x Dependency

    JSTL 1.2.x comes with various tags that serve different purposes. Depending on which tags you plan to use you will need certain dependencies.

    You don't need any other dependencies if you plan to use just the core, format, function tags or just Unified EL (expression language).

    If you plan to use the sql tags then you'll need the JDBC connector JAR file for your database. You can place this connector JAR in your web container's lib folder.

    If you plan to use the xml tags then you dependent JAR files Xalan.jar, Serializer.jar.

    If you plan to use JSF along with JSTL 1.2.x then you need JSF version 1.2 which is compatible with JSP 2.1

    If I've missed any other dependencies, then you'll get an exception error message such as "ClassNotFound", "Method Not Found" etc, examine the exception, to find out which package the class or method belongs to and then search for the distributions containing those class or method files. Get the distribution version that is compatible with the JSTL version you are using.

    standard.jar is already included in jstl-1.2.jar, if you still have the standard.jar in WEB-INF/lib from and older installation of JSTL then you can remove it because it is already part of JSTL 1.2 JAR fil.e

    Dependencies for previous version (JSTL 1.1.2) is here.

    Return to Top

  • JSTL 1.2 Usage Guide

    The tag library you plan to use must be defined in each JSP page the tags appear on. If you are familiar with XML terminology this is known as name space declartion. By declaring a namespace prefix of say "c"and associating it with a taglib URI, you are basically indicating that all tags that start with c: are JSTL core tags.

    The taglib URIs are different across different versions of JSTL. Make sure that the taglib URIs you use in the JSP files match with the JSTL version you are using. The JSTL 1.2 taglib URIs are the same as JSTL 1.1.x taglib URIs.

    To see JSTL 1.1.x the taglib URIs, click on the tag library of your choice. For example see the core taglib URI.

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

    JSTL fmt:
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

    JSTL sql:
    <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>

    JSTL XML:
    <%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>

    JSTL Functions:
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

Return to Top

JSTL 1.2 Examples

JSTL 1.2 uses Unified EL, this means the EL in the JSTL,JSP are compatible with JSF EL.

JSTL core tags
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <html>
    <head><title>JSTL 1.2 core tag example</title></head>
    <body>
        <c:set var="mysStringVariable" value="Hello World!"/>
        <c:out var="myStringVariable"/>
    </body>
    </html>


JSTL 1.2 EL
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head><title>JSTL 1.2 EL example</title></head>
    <body>
        ${requestScope.myStringVariable}

        ${ 100/5 }

        ${ 9 <42 }
    </body>
    </html>

    Notice that no taglib definitions are required if only EL is written in a JSP page.


JSTL 1.2 Function tags
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
    <html>
    <head><title>JSTL 1.2 function tags </title></head>
    <body>
        ${fn:toUpperCase(requestScope.myStringVariable)}

        ${ fn:escapeXml(sessionScope.siteUrl) }

    </body>
    </html>


JSTL format ( fmt ) tags
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    <html>
    <head><title>JSTL 1.2 format tag example</title></head>
    <body>
        <fmt:formatDate value="${dateVariable}" pattern="yyyy/MM/dd"/>
    </body>
    </html>


Sometimes the format tag doesn't work as expected. Especially date formatting doesn't work. This is because the format tag requires the locale to be set. For example:
<fmt:setLocale value="en-US"/>

See the API doc for setting the locale to the languageCode-CountryCode of your choice.

XML Tags Example - usage with external XML and XSTL files.
File: mydata.xml
<?xml version="1.0" encoding="UTF-8"?>
<books>
    <book>
        <title>JSTL 1.2 Reference</title>
        <author>Rashmi</author>
    </book>
</books>


File: transform_data.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="book">
        <ul>
            <li>
                <xsl:value-of select="title" />
            </li>
            <xsl:apply-templates select="book" />
        </ul>
    </xsl:template>
</xsl:stylesheet>


File: index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<html>
<head><title>JSTL 1.2 XML Tags usage example</title></head>

<body>

<c:import url="mydata.xml" var="xml"/>
<c:import url="transform_data.xsl" var="xsl"/>
<x:transform doc="${xml}" xslt="${xsl}"/>

Verify that the path (absolute, relative, context relative) of the above imported files is correct.
Use forward slash / as a path separator.    
</body>
</html>


XML Tag examples, with transformation within the JSP page (no external XSLT stylesheet).
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
    <c:import url="/data.xml" var="xmlData"/>
    <x:parse xml="${xmlData}" var="parsedDoc"   />
    <html>
        <head><title>JSTL 1.2 XML Transform Example</title></head>
        <body>
            <x:forEach select="$parsedDoc/books/book">
                <x:out select="title"/>
                <br/>
                <x:out select="author"/>
            </x:forEach>
        </body>
    </html>


SQL tags example
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
        <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
        <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
        <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
        <!-- Define this JNDI, JDBC data source  in context.xml or ROOT.xml -->
        <sql:setDataSource dataSource="jdbc/MyDB"/>
        <sql:query var="bookTitles">
            SELECT title
            FROM book;
        </sql:query>

        <ul>
            <c:forEach var="title" items="${bookTitles.rows}">
                <li>
                        <c:out value="${fn:toLowerCase(title)}" escapeXml="true"/>
                </li>
            </c:forEach>
        </ul>
Important Note: There are differences in configuration and JSTL URIs across different versions of JSTL. For example the taglib URIs for JSTL 1.0 is different from that of JSTL 1.1.x and 1.2, also the compatibility with the web server version, web.xml version, dependency configuration is different across versions. It is a good idea to check your version of JSTL and make sure everything is configured correctly for your version. If JSTL is configured incorrectly for its version, then these examples won't work. See configuration guides for JSTL 1.1.x and JSTL 1.2 for more details.

Return to Top

Troubleshoot JSTL 1.2

Unified EL is not evaluating

If the Unified EL does not evaluate this is most likely a compatibility issue. If you are upgrading from a previous version of JSTL it is recommended that you start fresh on a clean slate. Verify that the Web Container and your deployment descriptor (web.xml) is compatible with Servlet Specification 2.5 or higher. See compatibility for more details.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值