标签:自定义标签
(1)首先我们看看传统便签的类结构图,其中包括了简单标签
由上图可知,传统标签较为复杂,然而简单标签较为简单。
(2)开发流程
(1)写一个类实现Tag接口 (2)写一个tld文件,描述写好的类 (3)在jsp页面中引入tld
继承tag接口类
package tag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;
public class tag implements Tag {
private PageContext pc =null;
public int doEndTag() throws JspException {
// TODO Auto-generated method stub
return 0;
}
public int doStartTag() throws JspException {
// TODO Auto-generated method stub
String ip = pc.getRequest().getRemoteAddr();
try {
pc.getOut().write(ip);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
public Tag getParent() {
// TODO Auto-generated method stub
return null;
}
public void release() {
// TODO Auto-generated method stub
}
public void setPageContext(PageContext arg0) {
// TODO Auto-generated method stub
pc = arg0;
}
public void setParent(Tag arg0) {
// TODO Auto-generated method stub
}
}
<?xml version="1.0" encoding="UTF-8"?> <taglib version="2.0" 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 web-jsptaglibrary_2_0.xsd"> <tlib-version>1.0</tlib-version> <short-name>MyTag</short-name> <uri>http://www.mytag.com/MyTag</uri> 引入的uri <tag> <name>showip</name> 自定义标签的名字 <tag-class>tag.tag</tag-class> 自定义标签所定义的类 <body-content>empty</body-content> </tag> </taglib>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://www.mytag.com/MyTag" prefix="MyTag" %> 引入的tld
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
java你的IP地址<%=request.getRemoteAddr() %>><br>
自定义标签你的IP地址<MyTag:showip></MyTag:showip>>
</body>
</html>
标签:自定义标签
原文地址:http://blog.csdn.net/xiaomin1992/article/details/46361329