2012-06-01 36 views
3

我想用我的jsp文件文件是這樣的:JSP 2.0 - 使用標記文件的JSP文件中沒有.TLD聲明

<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %> 

然而,在所有的30 +的例子我已經可以看到,每個人都使用簡單的jsp語法,而不是jsp文檔語法。類似這樣的:

<jsp:root 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" 
    xmlns:fmt="http://java.sun.com/jsp/jstl/fmt" 
    xmlns:t="/WEB-INF/tags" 
    xmlns="http://www.w3.org/1999/xhtml" 
    version="2.0"> 

根本行不通。所有駐留在/ WEB-INF /標籤中的標籤文件都不在頁面上。只有當我定義一個tld文件並列出所有標籤時,纔可以在頁面上訪問它們。是否有可能避免tld聲明並仍然在jsp文檔頁面中使用標籤文件?

回答

5

1在您創建標籤目錄WEB-INF /目錄

2創建sample.tag文件,其中的標籤是:

<%@ attribute name="exampleAttribute" required="true" type="java.lang.String" description="Example attribute" %> 

<c:out value="${exampleAttribute}"> 

3申報標籤庫在你的jsp要使用它:

<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %> 

4使用它:

<tags:sample exampleAttribute="Hello from custom tag!"/> 

我想你應該有Web應用程序2.5的版本。僅從該版本考慮支持JSP 2.0(簽入web.xml)。

+1

JSP頁面和文檔具有不同的語法。您使用了頁面語法,但我需要使用jsp文檔語法(使用純xml)編寫的相當於 的'<%@ taglib prefix =「tags」tagdir =「/ WEB-INF/tags」%>「。如果我在我的文檔中使用您的語句,我只會得到一個異常,因爲它不是一個有效的jsp文檔代碼。 – Shajirr

+0

非常適合我! –

5

您需要在xmlns屬性中放置前綴「urn:jsptagdir:」。在你的情況,

<jsp:root 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" 
    xmlns:fmt="http://java.sun.com/jsp/jstl/fmt" 
    xmlns:t="urn:jsptagdir:/WEB-INF/tags" 
    xmlns="http://www.w3.org/1999/xhtml" 
    version="2.0"> 

您還可以使用前綴「甕:jsptld:」指定TLD的位置。有關更多詳細信息,請參閱http://docs.oracle.com/javaee/1.4/tutorial/doc/JSPX3.html中的「聲明標籤庫」部分。