---
title: "java自定义标签的开发步骤"
author: "dou_shini8820"
date: "2012-11-03T16:23:27Z"
tags: []
categories: ["Web前端"]
slug: "java"
draft: false
lastmod: "2012-11-03T17:35:14Z"
id: 1715020
url: "https://doushini.iteye.com/blog/1715020"
---

在jsp页面记录访问者的IP，可使用如下java代码：

 

```
<%
String ip = request.getRemoteAddr();
out.print(ip);
%>
```

 自定义标签的作用就是把jsp上的java代码移植到java文件中。

 

步骤：

1.写一个类来实现上述java代码，这个类要实现Tag接口，通常我们继承tagSupport这个类，

这个接口里有doStartTag().doEndtag(),setPageContext()等方法，当tomcat编译jsp页面开始一个标签时就执行doStartTag中的方法，setPageContext()在这之前把jsp上的page传递给“标签处理器类”了，也就意味我们可以获得request等对象了。

 

2.对于自定义标签我们需要一个描述标签意义的文件，这个文件是放在WEB-INF下的tld文件。

文件中有&lt;Tag&gt;的描述，需要设置相应的标签处理器类和uri-----以便在jsp页面导入。

 

3.在jsp页面使用taglib引入我们的uri,并指定prefix（通常和tld的文件名相同）。

 

4.在jsp页面使用我们开发的标签&lt;pp:showIP/&gt;

 

 

 

二。开发if。。。else自定义标签

 

首先思考，当jsp页面的if执行了，那么就要通知else这个java类不要执行，因此他们要有个共同的变量来标识，从而需要使用一个父标签来控制这个变量。这就类似与jstl中的

 

```
<c:choose>
	<c:when test="${user != null}">
		aaaaaaaaaa
	</c:when>
	<c:otherwise>
		bbbbbbbbbbb
	</c:otherwise>
</c:choose>
```

 

 

1.创建3个类ChooseTag，WhenTag，OtherwiseTag。

ChooseTag：中的isDao用来标识when标签和otherwise标签是否执行了，如果其中任何一个执行了，另外一个就不需要执行了。为了控制when和otherwise，需要doTag，调用getJspBody().invoke(null);

 

 

```
private boolean isDo;

	public boolean isDo() {
		return isDo;
	}

	public void setDo(boolean isDo) {
		this.isDo = isDo;
	}

	@Override
	public void doTag() throws JspException, IOException {
		this.getJspBody().invoke(null);
	}
```

 

 

 WhenTag:有一个test变量，是boolean型的，when执行前要判断其父标签的isDo是否为true，不为true且test变量为true时才执行。this.getParent()可以获取父标签，需要强制转换成ChooseTag。

 

when执行了后，要把isDo设置为true。

 

```
private boolean test;

	public void setTest(boolean test) {
		this.test = test;
	}

	@Override
	public void doTag() throws JspException, IOException {
		ChooseTag parent = (ChooseTag) this.getParent();
		if(test && !parent.isDo()){
			this.getJspBody().invoke(null);
			parent.setDo(true);
		}
	}
```

  OtherwiseTag：

 

 

```
@Override
	public void doTag() throws JspException, IOException {
		ChooseTag parent = (ChooseTag) this.getParent();
		if(!parent.isDo()){
			this.getJspBody().invoke(null);
			parent.setDo(true);
		}
	}
```

 

 

2.在WEB-INF下建luo.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>
        <name>choose</name>
        <tag-class>com.luo.web.tag.ChooseTag</tag-class>
        <body-content>scriptless</body-content>
    </tag>
    <tag>
        <name>when</name>
        <tag-class>com.luo.web.tag.WhenTag</tag-class>
        <body-content>scriptless</body-content>
        <attribute>
        	<name>test</name>
        	<required>true</required>
        	<rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
    <tag>
        <name>otherwise</name>
        <tag-class>com.luo.web.tag.OtherwiseTag</tag-class>
        <body-content>scriptless</body-content>
    </tag>
</taglib>
```

 其中&lt;body-content&gt;scriptless&lt;/body-content&gt;的scriptless包括JSP,表示有标签体，when标签还有个属性test。

3.在jsp最上面引入luo.tld

 

```
<%@taglib uri="http://baidu.com" prefix="c"%>
```

uri也可以表示成/WEB-INF/luo.tld 

 

三、打包

1、新建普通java工程，把刚才写的几个tag类连同包一起copy过来，然后再建立META-INF文件夹，把luo.tld copy进去，报错不用管

 

 

2、将工程导出jar格式，其中的.project文件和.classpath不用导