一、web.xml添加自定义标签文件
<jsp-config>
<taglib>
<taglib-uri>/WEB-INF/bing</taglib-uri>
<taglib-location>/WEB-INF/bing.tld</taglib-location>
</taglib>
</jsp-config>
二、bing.tld
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.3</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>bean</short-name>
<uri>bing</uri>
<tag>
<name>hello</name>
<tag-class>com.bing.tags.HelloTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue><!--是否接受动态的值,比如el表达式 -->
<type>java.lang.String</type>
</attribute>
</tag>
</taglib>
解释:uri:用于页面引入的地址。
tag中的name属性:定义了我们的tag名称,在后面会用到。
tag中的tag-class属性:指定了我们这个tag的实现类。
tag中的bod-ycontent属性:指定我们的页面内容是什么性质的。(注意:在jsp开发中这里必须写JSP)
tag中的attribute属性:定义了我们的这个tag可能有的属性。
attribute中的name属性:指定了属性的名称。
attribute中的required属性:表示这个属性是否是必须的。
attribute中的rtexprvalue属性:表示这个属性是否可以用EL表达式或者其他jsp程序段的结果输出。
三、标签类
package com.bing.tags;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
/**
* SKIP_BODY隐含0 :跳过了开始和结束标签之间的代码。
EVAL_BODY_INCLUDE隐含1:将body的内容输出到存在的输出流中
SKIP_PAGE隐含5 : 忽略剩下的页面。
EVAL_PAGE隐含6:继续执行下面的页
EVAL_BODY_AGAIN 2
EVAL_BODY_INCLUDE:把Body读入存在的输出流中,doStartTag()函数可用
EVAL_PAGE:继续处理页面,doEndTag()函数可用
SKIP_BODY:忽略对Body的处理,doStartTag()和doAfterBody()函数可用
SKIP_PAGE:忽略对余下页面的处理,doEndTag()函数可用
EVAL_BODY_TAG:已经废止,由EVAL_BODY_BUFFERED取代
EVAL_BODY_BUFFERED:申请缓冲区,由setBodyContent()函数得到的BodyContent对象来处理tag的body,如果类实现了BodyTag,那么doStartTag()可用,否则非法
一个扩展BodyTagSupport的自定义标记的生命周期如下:
1.创建标记
2.调用Setter方法
3.调用doStartTag()方法
4.调用setBodyContent()方法
5.调用InitBody()方法
6.处理标记的Body
7.doAfterBody();根据返回值,如果为EVAL_BODY_AGAIN,继续执行6,如果不是,执行8
8.调用doEndTag()方法
9.判断标记是否需要重用,如果要,执行4;否则执行release()方法。
* @author abc
*
*/
public class HelloTag extends BodyTagSupport{
@Override
public void setBodyContent(BodyContent b) {
try {
b.append('c');
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void doInitBody() throws JspException {
System.out.println("doInitBody");
}
@Override
public int doAfterBody() throws JspException {
return EVAL_PAGE;
}
@Override
public void release() {
super.release();
}
@Override
public BodyContent getBodyContent() {
return super.getBodyContent();
}
@Override
public JspWriter getPreviousOut() {
return super.getPreviousOut();
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
try {
this.name =(String) ExpressionEvaluatorManager.evaluate("name", name.toString(), Object.class, this, pageContext);
} catch (JspException e) {
e.printStackTrace();
}
}
@Override
public int doStartTag() throws JspException {
JspWriter out=pageContext.getOut();
try {
out.println("hello"+name);
} catch (IOException e) {
e.printStackTrace();
}
return 1;
}
@Override
public int doEndTag() throws JspException {
release();
return 6;
}
}
四、使用 <%@ taglib prefix="bing" uri="/WEB-INF/bing" %> 注意这里的uri是web.xml中定义的taglib-uri值<bing:hello name="${name}">aaaaaaaaaaaaaa</bing:hello><br/>bbbb