---
title: "java 自定义标签 显示时间 小实例"
author: "java_bckf"
date: "2014-01-21T15:13:52Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2014-01-21T15:13:52Z"
id: 2007788
url: "https://java-bckf.iteye.com/blog/2007788"
---

准备工作：创建一个Dynamic Web project项目并建好相应的文件夹及包

1.要创建一个java类，这个类继承Tag的默认实现类TagSupport，然后覆盖doStartTag()方法

```
package cn.test.demo;

import java.io.IOException;
import java.util.Date;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;
/**
 * 
 * @author 飞鸟
 *         Date:  2014-1-21
 * project_name:  firsttag
 *    file_name:  Demo1.java
 *
 */
public class Demo1 extends TagSupport {

	@Override
	public int doStartTag() throws JspException {
		// TODO Auto-generated method stub
		JspWriter out=pageContext.getOut();
		try {
			out.write(new Date().toLocaleString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return Tag.EVAL_BODY_INCLUDE;
	}

}
```

 

2.写一个tld文件，这个没有直接创建的，在项目的WebContent-&gt;WEB-INF下创建一个file,filename为test.tld

  代码呢去tomcat下面复制，找到tomcat的路径D:\\apache-tomcat-6.0.33\\webapps\\examples\\WEB-INF\\jsp2

找到文件jsp2-example-taglib.tld，复制头粘进自己建的.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>A tag library exercising SimpleTag handlers.</description>
    <tlib-version>1.0</tlib-version>
    <short-name>SimpleTagLibrary</short-name>
    <uri>http://baidu.com</uri>
    <tag>
	<description>Outputs Hello, World</description>
        <name>helloWorld</name>
	<tag-class>cn.test.demo.Demo1</tag-class>
	<body-content>empty</body-content>
    </tag>
    </taglib>
```

 

在此代码中关注一下几个标签即可：

 &lt;uri&gt; 标签    在此记住uri，因为在jsp文件中会有引入标签头中用到，uri的写法可以有两种：a.任意一个网址eg:http://baidu.com,但是必须保持在tld文件中的uri和jsp文件引入标签的头部分的uri一致。b.写tld文件在web项目中的路径，eg:/WEB-INF/test.tld 同上jsp和tld文件中uri一致。

&lt;name&gt;标签  这个name标签就是自定义标签的name，在jsp中引用会用到的，此处不多做介绍

&lt;tag-class&gt;标签   这个标签对中添加的是继承TagSupport类的那个java类的路径

&lt;body-content&gt;标签 这个标签对中添加的内容分两种情况添加：a.当本项目中只有一个自定义标签时，标签对中添加empty；b.当本项目中有多个自定义标签时,标签对中填写JSP(jsp必须大写的);  
3.创建一个jsp文件

```
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://baidu.com" prefix="infs" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<infs:helloWorld/>
</body>
</html>
```

 在此文件中注意&lt;%@ taglib uri="[http://baidu.com](http://baidu.com/)" prefix="infs" %&gt;这句代码的意思是引入自定义标签到jsp页面，uri必须与tld文件中的uri一致，prefix的值就是自定义标签的前缀，在jsp的页面中会有标签的使用的，如本例子中的&lt;infs:helloWorld/&gt;，其中infs是自定义标签前缀，后面的helloWorld是tld文件中标签&lt;name&gt;中的值，两者保持一致就行了。