首先定義use.tag,存放目錄在/WEB-INF/tags/use.tag;
<%@ tag body-content="empty" trimDirectiveWhitespaces="true" pageEncoding="UTF-8"%>
<%@ attribute name="username" required="true" rtexprvalue="true" %>
<%@ attribute name="password" required="true" rtexprvalue="true" %>
<div>
<p>This is a test!</p><br/>
<input name="username" value="${username}"/>
<input name="password" value="${password}"/>
</div>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><!-- c標籤 -->
<%@ taglib tagdir="/WEB-INF/tags/" prefix="xs" %> <!--自定義標籤tag存放目錄 自定義標籤tag的前綴-->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><body><xs:use username="beibei" password="password" />
</body></html>
web.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<welcome-file-list>
<welcome-file>index.html</welcome-file>e>
</welcome-file-list>
<jsp-config>
<taglib>
<taglib-uri>/WEB-INF/tags/use.tld</taglib-uri>
<taglib-location>/WEB-INF/tags/use.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
關於下面的異常:問題原因:web.xml中配置 <taglib-uri>/WEB-INF/tags/use.tag</taglib-uri>則出現下面圖片中的異常。
正確的配置:需要明白自定義tld和自定義tag的區別,混亂使用了。在jsp-config中定義taglib標籤的方式只有在使用自定義tld標籤的時候才需要使用,配置如上面的web.xml配置即可。而對於.tag的標籤,則不需要在web.xml中定義相應的jsp配置,因爲在jsp裏面就已經定義了<%@ taglib tagdir="/WEB-INF/tags/" prefix="xs" %> <!--自定義標籤tag存放目錄 自定義標籤tag的前綴-->。