标签库组成:
1。标签处理程序类----实现标签所代表功能的程序段
2。标签库描述文件(tld)---描述标签处理程序与前端标签的匹配
3。标签指示----用在jsp显示端的
要实现一个简单的,计算,字符串长度的标签。
(一):标签处理程序:
package taglib;
import org.apache.log4j.Logger; 引用log4j库
/**
*
* @author zwc
*
*/
public class StringUtils {
private static Logger logger = Logger.getLogger(StringUtils.class);
public static Integer getStringLength(String str){ 注意:这里的方法,必须都是静态的,并且,应该是public的
logger.info("输入值:" + str);
if(str == null){
return 0;
}
return str.length();
}
}
(二)标签库,描述文件tld
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Copyright 2004 The Apache Software Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<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>A tag library exercising function handlers.</description>注意:该属性是可选择,属性,为了对该标签库的描述
<tlib-version>1.0</tlib-version>注意:这里是必选值,否则,调用出错。
<short-name>test</short-name>注意,该属性是可选择属性,因为在jsp页面定义了前缀,这里在定义一个,简略名,不知是什么意思。
<uri>/test-1.0</uri>注意:这里定义的路径,表示,jsp找到该标签的唯一路径
<function>
<description>测试: 与 el 结合使用 如 ${test:getStringLength("XX")}</description>
<name>getStringLength</name> 注意,这里,定义的是,标签对应的函数名
<function-class>taglib.StringUtils</function-class>注意:这里,给出了,该类,所在的路径,全路径
<function-signature>java.lang.Integer getStringLength(java.lang.String)</function-signature>
</function>
</taglib>
(三)前端jsp,应用
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="test" uri="/test-1.0"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Error</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
Error<br>
<jsp:useBean id="user" class="bean.User"/>
<jsp:setProperty property="name" value="abc" name="user"/>
<jsp:getProperty property="name" name="user"/>
Test: ${test:getStringLength("yuanfen860913")}
</body>
</html>
(四) web.xml中声明该标签,可以,声明,也可以不用,声明,因为,默认的,会去找/WEB-INF 下的,所有tld,标签库描述文件,然后自动加载
<jsp-config>
<taglib>
<taglib-uri>/test-1.0</taglib-uri>
<taglib-location>test-1.0.tld</taglib-location>
</taglib>
</jsp-config>
(五)运行结果:
Error
abc Test: 13