JSP简单的一个自定义标签

来源:互联网 发布:aws ec2 centos 编辑:程序博客网 时间:2024/06/04 19:14

这个步骤是麻雀虽小,五脏俱全。

步骤

第一步:编写一个实现tag接口的java类(我们这里继承一个已经实现Tag接口的类TagSupport.java,然后重写里面的do startTag()方法)
package biaoqianTag;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class tag  extends TagSupport{

    //其实在调用该方法之前,就设置好了pageContext对象。
    @Override
    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 (Exception e) {
         //不能顾打印异常,需要抛出,因为没有处理的地方。
            throw new RuntimeException();
        }
        return super.doStartTag();
    }

}

第二步:.在tld文件中对标签处理器进行描述(tld文件放在WEN-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>SimpleTagLibrary</short-name>
<!--给这个自定义的标签设置一个uri,在页面上引入使用 -->
    <uri>http://fyq/jsp2-example-taglib</uri>
    <tag>
<!-- 标签的名字,在页面上使用的时候,加上在页面上起的前缀 -->
        <name>viewIP</name>
<!-- 实现Tag接口的类的绝对路径 -->
        <tag-class>biaoqianTag.tag</tag-class>
        <body-content>empty</body-content>
    </tag>
</taglib>
第三步:在页面上利用标签引入这个tld文件,并且起前缀。
<%@ page language="java" isELIgnored="false" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="http://fyq/jsp2-example-taglib" prefix="fyq"%>
<!DOCTYPE html>
<html>
  <head>
    <title>自定义标签</title>    
  </head>
  <body>
  你的ip是:
  <fyq:viewIP/>
  </body>
</html>

1.编写一个实现tag接口的java类(我们这里继承一个已经实现类Tag接口的类TagSupport.java)
package biaoqianTag;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class tag  extends TagSupport{

    //其实在调用该方法之前,就设置好了pageContext对象。
    @Override
    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 (Exception e) {
            throw new RuntimeException();
        }
        return super.doStartTag();
    }

}

2.在tld文件中对标签处理器进行描述(tld文件放在WEN-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>SimpleTagLibrary</short-name>
    <uri>http://fyq/jsp2-example-taglib</uri>
    <tag>
        <name>viewIP</name>
        <tag-class>biaoqianTag.tag</tag-class>
        <body-content>empty</body-content>
    </tag>
</taglib>
3.在页面上利用标签引入这个tld文件,并且起前缀。
<%@ page language="java" isELIgnored="false" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="http://fyq/jsp2-example-taglib" prefix="fyq"%>
<!DOCTYPE html>
<html>
  <head>
    <title>自定义标签</title>    
  </head>
  <body>
  你的ip是:
  <fyq:viewIP/>
  </body>
</html>
0 0
原创粉丝点击