0%

TLD自定义标签之基础入门篇

tld,是taglib description 的缩写,其自定义标签一般用于jsp页面,tld其作用一般是在web项目中结

jstl、c标签等用于有效性判断、权限判断等方面,对前端的一些页面标签起到约束、限制的作用。

1、tld说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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″>

<tlib-version>1.0</tlib-version>
<short-name>isnull</short-name><!– 定义标签使用的短名称 –>

<!– 自定义标签的形参都是域里面传递的参数值;自定义标签基本使用在jsp页面上 –>
<function>
<description>判断传递内容是否为空</description><!– 对该标签的说明 –>
<name>hasvalue</name><!– 定义标签名,放在短标签之后 –>
<function-class>util.Tld_util</function-class><!– 标签处理域值的类路径 –>
<function-signature>boolean isnull(java.lang.String)</function-signature><!– 标签处理域值的具体的类方法 –>
<example>${isnull:hasvalue(obj1)}</example><!– 自定义标签的使用示范 ,域参数会自动传递到具体的方法里面–>
</function>
</taglib>

2:调用

1
2
3
4
5
6
7
8
9
<c:choose>
<c:when test=”${isnull:hasvalue(${tid)}”>
<p>tld的值为123</p>
</c:when>
<c:otherwise>
<p>tld的值不是123</p>
</c:otherwise>
</c:choose>
<p>获取域值:${testtld}</p>