一、在Tomcat中安裝JSTL 前提 OS: WIN2003;Tomcat5.5;tomcat已經配置好。 1.準備jstl 到http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/下載jakarta-taglibs-standard-1.1.2.zip 解壓后成為jakarta-taglibs-standard-1.1.2 2.準備web開發目錄 比如我的web目錄系統默認目錄%tomcat_home%\webapps\ROOT下,工作目錄為ROOT,也可以自定義工作目錄,但必須要保證工作目錄下建立WEB-INF\lib,WEB-INF\classes 3.拷貝.jar文件 將jakarta-taglibs-standard-1.1.2\lib\下的兩個jar文件:standard.jar和jstl.jar文件拷貝到工作目錄的\WEB-INF\lib\下 4.拷貝.tld文件(可選) 將jakarta-taglibs-standard-1.1.2\tld\下的15個tld類型文件拷到"Working folder\WEB-INF\tld"下 5.在工作目錄下的\WEB-INF\下建立web.xml文件:(可選) <?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <taglib> <taglib-uri>http://java.sun.com/jstl/fmt</taglib-uri> <taglib-location>/WEB-INF/tld/fmt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/fmt-1_0</taglib-uri> <taglib-location>/WEB-INF/tld/fmt-1_0.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/fmt-1_0-rt</taglib-uri> <taglib-location>/WEB-INF/tld/fmt-1_0-rt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/core</taglib-uri> <taglib-location>/WEB-INF/tld/c.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/core-1_0-rt</taglib-uri> <taglib-location>/WEB-INF/tld/c-1_0-rt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/core-1_0</taglib-uri> <taglib-location>/WEB-INF/tld/c-1_0.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/sql</taglib-uri> <taglib-location>/WEB-INF/tld/sql.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/sql-1_0</taglib-uri> <taglib-location>/WEB-INF/tld/sql-1_0.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/sql-1_0-rt</taglib-uri> <taglib-location>/WEB-INF/tld/sql-1_0-rt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/x</taglib-uri> <taglib-location>/WEB-INF/tld/x.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/x-1_0</taglib-uri> <taglib-location>/WEB-INF/tld/x-1_0.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/x-1_0-rt</taglib-uri> <taglib-location>/WEB-INF/tld/x-1_0-rt.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/fn</taglib-uri> <taglib-location>/WEB-INF/tld/fn.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/permittedTaglibs</taglib-uri> <taglib-location>/WEB-INF/tld/permittedTaglibs.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://java.sun.com/jstl/scriptfree</taglib-uri> <taglib-location>/WEB-INF/tld/scriptfree.tld</taglib-location> </taglib> </web-app> 提示: In addition to declaring the tag libraries, tutorial examples access the JSTL API and implementation. In the Application Server, the JSTL TLDs and libraries are distributed in the archive <J2EE_HOME>/lib/appserv-jstl.jar. This library is automatically loaded into the classpath of all web applications running on the Application Server, so you don't need to add it to your web application. 以上摘自j2ee的tutorial,說明步驟四、五中的TLD文件注冊部分是可以省略的,WEB工程能自動的從jar包中加載相應的標簽庫。 6.建立一個名為test.jsp文件 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <%@ page contentType="text/html;charset=GB2312" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <head> <title>測試你的第一個使用到JSTL 的網頁</title> </head> <body> <c:out value="歡迎測試你的第一個使用到JSTL 的網頁"/> </br>你使用的瀏覽器是:</br> <c:out value="${header['User-Agent']}"/> <c:set var="a" value="David O'Davies" /> <c:out value="David O'Davies" escapeXml="true"/> </body> </html> 7.顯示結果: 歡迎測試你的第一個使用到JSTL 的網頁 你使用的瀏覽器是: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SailBrowser; .NET CLR 1.1.4322) David O'Davies 8.注意的問題 主要的一個問題還是路徑的問題,筆者配置了好幾次都不能成功,就是把工作目錄給弄錯了,把項目的目錄誤認為是工作目錄,一直在項目的目錄下配置文件,其實應該在工作目錄下配置,當然可以把工作目錄改成項目的目錄,問題也就可以解決了。 9.常出現的問題和提示: org.apache.jasper.JasperException: The absolute uri: http://java.sun.com/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application 出現這個問題主要一種可能是工作目錄的\WEB-INF\lib\下缺少standard.jar和jstl.jar文件,另外一種可能是web.xml沒有進行TAG的正確配置。 According to TLD or attribute directive in tag file, attribute value does not accept any expressions 應用部署運行的時候出現JSP異常, 發生在使用JSTL庫的時候: According to TLD or attribute directive in tag file, attribute value does not accept any expressions, 可能是因為使用了JSP2.0版本, 同時又沒有使用JSTL core庫的備用版本(RT庫), 以下有兩種處理方法: a. 修改web.xml. <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> 改為2.3版本的 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> b. 使用JSTL core RT庫 JSTL core庫的有兩種taglib偽指令, 其中RT庫即是依賴于JSP傳統的請求時屬性值, 而不是依賴于EL來實現(稱為EL庫.JSP2.0將支持EL) JSP中使用<%@ taglib uri=http://java.sun.com/jstl/core prefix="c"%>在2.3版本都可以,在2.4就不行了, 難道是版本不兼容嗎? 只要將 <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%> 改為 <%@ taglib uri=http://java.sun.com/jstl/core_rt prefix="c"%> 就沒有問題了 三、標簽的使用 C標準標簽庫 Taglib-http://java.sun.com/jstl/core 基礎: 1.jsp頁面引入C標簽庫: <@taglib uri=”http://java.sun.com/jstl/core” prefix=”c”> //引入標簽庫 前綴為c 2.c標簽庫的標簽列表 C標簽庫例舉 標簽名 用處 <c:choose> <c:forEach> <c:forTokens> <c:if> <c:import> <c:otherwise> <c:out> 把對象的數值輸出到JspWriter <c:p |