一、在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>
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:param>
<c:redirect>
<c:remove> 刪除某個變數或屬性
<c:url>
<c:when>
<c:set> 1. 用於在某個作用範圍(Request、Session、Application等)中設定某個值
2. 設定某個對象的屬性
<c:catch> 捕獲嵌在它裡面的標籤拋出異常
(1)<c:catch>標籤
作用: 捕獲嵌在它裡面的標籤拋出異常
<%@taglib uri=”http://java.sun.com/jstl/core” prefix=”c”%>
<%@page contentType=”text/html;charset=gb2312” %>
<html>
<head><title>c:catch標籤示範</title></head>
<body>
<c:catch var=”myException” > //設定異常控制代碼,好比JAVA的 Exception e一樣
<%
String str=”abc”;
int i =Integer.parseInt(str); //此處轉換產生異常,因為abc字串不能轉換成數值
%>
</c:catch>
異常:<c:out value=”${myException }”/>
</body>
</html>
(2)<c:set>標籤
作用: 1.用於在某個作用範圍(Request、Session、Application等)中設定某個值
2.設定某個對象的屬性
<%@taglib uri=”http://java.sun.com/jstl/core” prefix=”c”%>//引入標籤庫,設定首碼
<%@page contentType=”text/html;charset=gb2312”%>
<html>
<head><title>示範c:set標籤</title></head>
<body>
<c:set var=”objInRequest” value=”abcStr” scope=”request” />
//相當於request.setAttribute(“objInRequest”,”abcStr”)
<c:out value=”${objInRequest}”/>
//在request中擷取objInRequest屬性,將其值輸出到JspWriter
</body>
</html>
User.java //這是一個javabean對象的定義
package dev;
public class User{
public int id;
public String username;
public String password;
//public TYPE getter….
//public void setter….
}
//JSP頁面
<%@taglib uri=”http://java.sun.com/jstl/core” prefix=”c”%>
<%@page contentType=”text/html;charset=gb2312”%>
<jsp:useBean id=”userHandle” class=”dev.User”/>
<html>
<head><title>示範c:set標籤</title></head>
<body>
<c:set target=”${userHandle}” property=”id” value=”123”/>
<c:set target=”${userHandle}” property=”username” value=”lindeqiang”/>
<c:set target=”${userHandle}” property=”password”>
This is my password //注意,這裡的password的value的值也可以賦值在<c:set的標籤體中
//寫成<c:set target=”${userhandle}” property=”password” value=”123456”/>也可以
</c:set>
ID:<c:out value=”${ userHandle .id}”/><br>
User:<c:out value=”${userHandle.username}”/><br>
PWD:<c:out value=”${userHandle.password}”/><br>
</body>
</html>
3.<c:out>標籤
作用: 把對象的數值輸出到JspWriter
屬性:
屬性說明
屬性 類型 描述
value Object 計算的運算式
escapeXml Boolean 是否將轉換成字元實體代碼,預設為true
default Object D當value的對象不存在時(null),就輸出這個default的值