日期:2014-05-16  浏览次数:20409 次

为JSP自定义你自己的标签吧
/*
实现功能:自己来定义一个标签,用于jsp中

日期:20130930

作者:烟大阳仔

*/
1.编写一个实现tag接口的JAVA类

	public int doStartTag() throws JspException {
		
		
		HttpServletRequest request=(HttpServletRequest)this.pageContext.getRequest();
		JspWriter out=this.pageContext.getOut();
		String ip=request.getRemoteAddr();
		try {
			out.print(ip);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		return super.doStartTag();
	}
	
2.在tld文件中对标签处理器进行描述(tld文件的位置WEB-INF里面)
<?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>TagLib</short-name>
    <uri>/TagLib</uri>
    <tag>
        <name>VeiwIP</name>
        <tag-class>cn.com.web.tag.TagLibIP</tag-class>
        <body-content>empty</body-content>
    </tag>
</taglib>

3.在jsp页面中使用标签
<body> 
    	你的IP地址是:<TagLib:VeiwIP/>
  </body>