自定义标签继承树:
自定义标签类----->继承SimpleTagSupport------>实现SimpleTag接口
自定义标签开发步骤
1)编写一个普通的java类,继承SimpleTagSupport类,叫标签处理器类
/**
* 标签处理器类
* @author APPle
* 1)继承SimpleTagSupport
*
*/
public class ShowIpTag extends SimpleTagSupport{
private JspContext context;
/**
* 传入pageContext,在jsp中的_jspService中自动调用
*/
@Override
public void setJspContext(JspContext pc) {
this.context = pc;}
/**
* 2)覆盖doTag方法,同样是自动调用
*/
@Override
public void doTag() throws JspException, IOException {
//向浏览器输出客户的ip地址
PageContext pageContext = (PageContext)context;
HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
}
}
2)在web项目的WEB-INF目录下建立itcast.tld文件,这个tld叫标签库的声明文件。(参考核心标签库的tld文件)
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<!-- 标签库的版本 -->
<tlib-version>1.1</tlib-version>
<!-- 标签库前缀,在jsp的taglib指令中需要引入 -->
<short-name>itcast</short-name>
<!-- tld文件的唯一标记,在jsp的taglib指令中需要引入 -->
<uri>http://gz.itcast.cn</uri>
<!-- 一个标签的声明 -->
<tag>
<!-- 标签名称 -->
<name>showIp</name>
<!-- 标签处理器类的全名 -->
<tag-class>gz.itcast.a_tag.ShowIpTag</tag-class>
<!-- 输出标签体内容格式(暂时写上scriptless) -->
<body-c