Java for Web學習筆記(二五):JSTL(1)使用JSTL__JAVA

來源:互聯網
上載者:User

在前面已經使用過JSTL,例如<c:url>,fn是JSTL的functionlibrary,而c是JSTL的tag library。使用它們,我們要告知解析器,如下:

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

這些directive相當於下面的。雖然prefix(或者說namespace)可以設定為不同,但是我們應當遵循TLD(Tag Library Descriptor)中的建議,方便其他人閱讀。

<jsp:root xmlns="http://www.w3.org/1999/xhtml" version="2.0"          xmlns:jsp="http://java.sun.com/JSP/Page"          xmlns:c="http://java.sun.com/jsp/jstl/core"          xmlns:fn="http://java.sun.com/jsp/jstl/functions">

在JSP檔案中,jsp tag是預設預設的,即無需給出prefix(namespace),當然,也可以寫為<jsp:include>,<jsp:forward>。

注意,uri是名字的約定,而不是真正的網址,只是用於作為TLD的唯一標識。當JSP解析器碰到taglib的directive,需使用URI獲得該tag library的TLD檔案。解析器首先搜尋Java EE容器的Jave EE specification的TLD檔案,包括JSP taglibrary,JSTL,和JavaServer Faces libraries;其次檢查在web.xml中在<jsp-config>中指定的<taglib>;再其次檢查應用/WEB-INF/lib中的JAR檔案是否還有TLD檔案,或者在/WEB-INF下或其子目錄下的TLD檔案;最後檢查是否作為web容器或者應用伺服器部分進行了封裝的TLD(此通常是自訂的web容器,使得應用和web容器綁定,無法移植)。

對於第二種的自訂方式,如下:

<jsp-config>...    <taglib>        <taglib-uri>http://www.example.org/xmlns/jsp/custom</taglib-uri>        <taglib-location>/tld/custom.tld</taglib-location>    </taglib>...</jsp-config>

Tomcat Server並不支援JSTL,因此在Maven的pom.xml檔案中需要引入,採用lib的jar方式提供JSTL的支援。注意,這裡的<scope>採用的是compile而不是provided,因為tomcat並不包含相關的libraries。

<dependency>    <groupId>javax.servlet.jsp.jstl</groupId>      <artifactId>javax.servlet.jsp.jstl-api</artifactId>      <version>1.2.1</version>      <scope>compile</scope>  </dependency><dependency>    <groupId>org.glassfish.web</groupId>      <artifactId>javax.servlet.jsp.jstl</artifactId>      <version>1.2.2</version>    <scope>compile</scope>    <exclusions>        <exclusion>            <groupId>javax.servlet</groupId>            <artifactId>servlet-api</artifactId>        </exclusion>        <exclusion>            <groupId>javax.servlet.jsp</groupId>            <artifactId>jsp-api</artifactId>        </exclusion>        <exclusion>            <groupId>javax.servlet.jsp.jstl</groupId>            <artifactId>jstl-api</artifactId>        </exclusion>    </exclusions>             </dependency>

JSTL裡面有5個tag庫,分布是: Core(c) Formatting(fmt) Functions(fn) SQL(sql) XML(x)

文法請參考見:http://docs.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/。沒有提供1.2的文檔,但是差別很小。一般而言,不建議使用SQL和XML。


相關連結: 我的Professional Java for Web Applications相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.