灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:3725回复:0

JSP技术中的开发自定义标签

楼主#
更多 发布于:2012-09-10 19:52

因为在JSP中不要使用一行java代码,所以说必须使用标签来移除显示的java代码,下面罗列一下标签的开发过程。

1.首先编写一个实现tag接口的java

package com.bird.web.tag;

import java.io.IOException;

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

/**
* 显示来访者IP的自定义标签
* @author Bird
*
*/
public class ViewIPTag extends TagSupport{

    private static final long serialVersionUID = 1L;
    
    
    @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 (IOException e) {
            throw new RuntimeException(e);
        }
        
        
        return super.DOStartTag();
    }
}
package com.bird.web.tag;

import java.io.IOException;

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

/**
* 显示来访者IP的自定义标签
* @author Bird
*
*/
public class ViewIPTag extends TagSupport{

       private static final long serialVersionUID = 1L;
      
      
       @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 (IOException e) {
                     throw new RuntimeException(e);
              }
            
            
              return super.DOStartTag();
       }
}


2.在tld文件里面对标签处理器类进行描述,tld文件的位置在web-inf下面可以在tomcat的webapps下面抄一下



<?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"
    xsichemaLocation="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>Bird</short-name>
    <uri>http://www.bird.net</uri>  
    <tag>
        <name>viewIP</name>
        <tag-class>com.bird.web.tag.ViewIPTag</tag-class>
        <body-content>empty</body-content>
    </tag>
    </taglib>
<?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"
    xsichemaLocation="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>Bird</short-name>
    <uri>http://www.bird.net</uri>
    <tag>
        <name>viewIP</name>
              <tag-class>com.bird.web.tag.ViewIPTag</tag-class>
              <body-content>empty</body-content>
    </tag>
    </taglib>

4.在界面里面使用tablib命令到导入和使用标签就可以了,



<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%@taglib uri="http://www.bird.net" prefix="bird" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>一个简单的自定义标签</title>
    
  </head>
  
  <body>
   您访问的IP为: <bird:viewIP/>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%@taglib uri=http://www.atcpu.com prefix="bird" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>一个简单的自定义标签</title>
  
  </head>

  <body>
   您访问的IP为: <bird:viewIP/>
  </body>
</html>




喜欢0 评分0
游客

返回顶部