jsp自定义标签页tag
1、WEB-INF下新建一个tags文件夹,创建tag文件
2、index.jsp文件头部添加自定义标签库
<%@ taglib tagdir="/WEB-INF/tags" prefix="mytags"%>
div.tag代码如下
<%@ tag pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ attribute name="color" type="java.lang.String" required="true"%>
<%
if (color != "red") {
color = "black";
}else{
}
%>
<style>
#red{
color: red;
}
</style>
<div id="${color}">
divContent
</div>
a.tag代码如下
<%@ tag pageEncoding="UTF-8" %>
<a>
<jsp:doBody></jsp:doBody>
</a>
index.jsp中使用
<body>
<mytags:div color="red">
</mytags:div>
<mytags:a>http://www.baidu.com</mytags:a>
</body>
效果
jsp自定义功能标签tld
1、在WEB-INF下新建tlds文件夹,创建fnc.tld文件
2、index.jsp头部添加声明
<%@ taglib prefix="fnc" uri="/WEB-INF/tlds/fnc.tld" %>
fnc.tld代码如下
<?xml version="1.0" encoding="UTF-8" ?>
<taglib 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-jsptaglibrary_2_0.xsd"
version="2.0">
<description>JSTL 1.1 functions library</description>
<display-name>JSTL functions cms</display-name>
<tlib-version>1.1</tlib-version>
<short-name>fnc</short-name>
<uri>http://java.sun.com/jsp/jstl/functionsc</uri>
<function>
<description>获取用户列表</description>
<name>getUsers</name>
<function-class>com.helper.UserHelper</function-class>
<function-signature>java.util.List getUsers()</function-signature>
<example>${fnc:getUsers()}</example>
</function>
</taglib>