jsp自定义tag标签
首先定义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的前缀-->。