zoukankan      html  css  js  c++  java
  • Rhythmk 一步一步学 JAVA(7): jsp 自定义标签

    1、实现Tag接口:

         TagSupport类实现了Tag接口,为我们提供了4个重要的方法(见表6-5)。

         1.1、 TagSupport类中的常用方法

                  int doStartTag():

    遇到自定义标签开始时调用该方法,

    其可选返回值如下。

    SKIP_BODY:表示不用处理标签体,

    直接调用doEndTag()方法

    EVAL_BODY_INCLUDE:正常执行

    标签体,但不对标签体做任何处理

    int doAfterBody():

    重复执行标签体内容的方法,
    其可选返回值如下。SKIP_BODY:表示不用处理标签体,直接调用doEndTag()方法EVAL_BODY_AGAIN:重复执行标签体内容

     

     

    int doEndTag():

    遇到自定义标签结束时调用该方法,

    其可选返回值如下。

    SKIP_PAGE:忽略标签后面的JSP

    内容,中止JSP页面执行

    EVAL_PAGE:处理标签后,继

    续处理JSP后面的内容

    void release():

    释放获得的所有资源

    参考代码:

    package com.rhythmk.web.tag;
    
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import javax.management.RuntimeErrorException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.jsp.JspException;
    import javax.servlet.jsp.JspWriter;
    import javax.servlet.jsp.tagext.TagSupport;
    
    /*
     *  输出当前时间
     * */
    public class ViewTimeTag extends TagSupport {
    
        @Override
        public int doStartTag() throws JspException {
        
            
            HttpServletRequest request=(HttpServletRequest) this.pageContext.getRequest();
            JspWriter out=this.pageContext.getOut();
            
            Date date=new Date();
            SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
            try {
                out.print(df.format(date));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                throw new  RuntimeException(e);
            }
            
            return super.doStartTag();
        }
    }
     
     

    2、创建 *.tld文件:

         路径:/WebRoot/WEB-INF/rhythmk.tld

        

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE taglib
              PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
          "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
    <taglib>
      <tlib-version>1.0</tlib-version>
        <jsp-version>1.2</jsp-version>
      <short-name>rhythmk</short-name>
      <uri>http://www.rhythmk.com</uri>
      <description>rhythmk library</description>
    
    
      <tag>
        <name>ViewTime</name>
        <tag-class>com.rhythmk.web.tag.ViewTimeTag</tag-class>
        <body-content>empty</body-content>
      </tag>
    </taglib>

    3、引入标记到JSP页码:

       

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    
    <%@ taglib  uri="http://www.rhythmk.com"  prefix="rhythmk"  %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>JSP 自定义标签 </title>
      </head>
      
      <body>
       当前时间为:
        <rhythmk:ViewTime/>
      </body>
    </html>

     简单标签体开发:

        继承 “SimpleTagSupport”  实现 doTag 方法:

    package com.rhythmk.web.tag;
    
    import java.io.IOException;
    import java.io.StringWriter;
    
    import javax.servlet.jsp.JspException;
    import javax.servlet.jsp.tagext.JspFragment;
    import javax.servlet.jsp.tagext.SimpleTagSupport;
    
    /*
     *  简单标签
     * */
    
    public class SimpleTagDemo1 extends SimpleTagSupport {
    
        @Override
        public void doTag() throws JspException, IOException {
            
            JspFragment jf=this.getJspBody();
            /* 循环输出标签内容5次 */
            for(int i=0;i<5;i++)
            {
                jf.invoke(null);
            }
            
            // 修改标签内容
        
            StringWriter sw=new StringWriter();
            // 获取标签内容
            jf.invoke(sw);
            String  content=  "<br/><b>"+ sw.toString()+"被修改了<b/>";
            // 写入页面
            this.getJspContext().getOut().write(content);
            
        }
    
        
    }

    配置如下:

    ...... 
     
    <tag>
        <name>SimpleTagDemo1</name>
        <tag-class>com.rhythmk.web.tag.SimpleTagDemo1</tag-class>
        <body-content>scriptless</body-content>
      </tag>
    
    ...... 
     

    调用方法:

    <%@ taglib  uri="http://www.rhythmk.com"  prefix="rhythmk"  %>
    <rhythmk:SimpleTagDemo1>
        标签内容
    </rhythmk:SimpleTagDemo1>
  • 相关阅读:
    安卓证书获取sha1的方法 实测有效 原创!!!
    KafKa_原理及项目整合
    微服务-小结
    bd——kafka
    Zookeeper应用——
    idea使用小结
    服务管理框架——Zookeeper
    中间件汇总——了解
    新篇章:大数据——Flume
    70-71中期10道基石
  • 原文地址:https://www.cnblogs.com/rhythmK/p/3298073.html
Copyright © 2011-2022 走看看