zoukankan      html  css  js  c++  java
  • 自定义标签之 多个标签的嵌套

    在实际开发中,往往需要多个标签的嵌套以完成一个任务,这样标签就存在父子关系。类似于下面:
    <mt:switch value="test">
        
    <mt:case value="test1">
                my    value is test1
        
    </mt:case>
        
    <mt:case value="test2">
                my value is  test2
        
    </mt:case>
    </mt:switch>
    在上面的标签中,<mt:switch>为父标签,<mt:case>为子标签,一个父标签可以嵌套多个子标签和HTML,Scriptlets等。
    下面就来写一个这样的标签
    ifTag.java
    package eflylab;
    import javax.servlet.jsp.*;
    import javax.servlet.jsp.tagext.*;
    import java.util.Hashtable;
    import java.io.Writer;
    import java.io.IOException;

    /**
     *if Tag
     *usage:<tag:if value=true>
     *      
     *      </tag:if>
     
    */

    public class IfTag extends BodyTagSupport
    {
        
    private boolean value;
        
    /**
         *设置属性的值。
         
    */

        
    public void setValue(boolean value)
        
    {
            
    this.value=value;
        }

        
        
    /**
         *doStartTag方法,如果value为true,那么
         *就计算tagbody的值,否则不计算body的值。
         
    */

        
    public int doStartTag() throws JspTagException
        
    {
            
    if(value)
            
    {
               System.out.println(
    "value is true");
               
    return EVAL_BODY_INCLUDE; 
             }
      
             
    else
             

               System.out.println(
    "value is false");
                
    return SKIP_BODY; 
             }
     
          }

             
         
        
    /**
         *覆盖doEndTag方法
         
    */

        
    public int doEndTag() throws JspTagException 
        
    {
            
    try 
            
    {  
                 
    if(bodyContent != null)  
                 
    {
                     bodyContent.writeOut(bodyContent.getEnclosingWriter()); 
                 }

            }
     
            
    catch(java.io.IOException e)
            
    {
                
    throw new JspTagException("IO Error: " + e.getMessage());  
            }
       
            
    return EVAL_PAGE;  
        }

          
    }

    Value为IfTag的属性。当为真时,那么就计算IfTagBody;如果为假,就忽略

    因为IfTag标签中要嵌套了一个子标签,这个子标签用于输出一些信息到客户端。
    下面的是IfTag中嵌套的子标签。
    OutTag.java

    package eflylab;
    import javax.servlet.jsp.*;
    import javax.servlet.jsp.tagext.*;
    import java.util.Hashtable;
    import java.io.Writer;
    import java.io.IOException;

    public class OutTag extends TagSupport
    {
        
    private Object value;    
        
        
    /**
         *覆盖doStartTag方法
         
    */

       
    public void setValue(Object value)
       
    {
            
    this.value=value;
       }

       
        
    public int doStartTag() throws JspTagException {
            
    return EVAL_BODY_INCLUDE;
        }

        
        
    /**
         *覆盖doEndTag方法
         
    */

        
    public int doEndTag()throws JspTagException
        
    {

            
    try
            
    {
                System.out.println(value);
                pageContext.getOut().write(value.toString());
                
            }

            
    catch(IOException ex)
            
    {
                
    throw new JspTagException("Fatal error:hello tag conld not write to JSP out");
            }

            
    return EVAL_PAGE;
        }

          
    }
    OutTag就是一个简单的不带Body的标签,它主要向客户端输出一些信息。
    下面是在标签描述文件中的声明,加入以下内容
         <tag>    
            
    <name>if</name>
        
    <tag-class>eflylab.IfTag</tag-class>
        
    <body-content>jsp</body-content>
            
    <attribute>
              
    <name>value</name>
              
    <required>true</required>
              
    <rtexprvalue>true</rtexprvalue>
          
    </attribute>
        
    </tag>
       
    <tag>    
            
    <name>out</name>
        
    <tag-class>eflylab.OutTag</tag-class>
        
    <body-content>jsp</body-content>
            
    <attribute>
              
    <name>value</name>
              
    <required>true</required>
              
    <rtexprvalue>true</rtexprvalue>
          
    </attribute>
         
    </tag> 
    测试代码:
    <%@ taglib uri="/demotag" prefix="mt" %>
    <html>
    <head>
    <title>vcorwork  tag</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    </head>
    <body>
    <HR>
    协作标签
    <br>
     
    <%
     
    boolean test=true;
     
    String outValue="HelloWorld!";
     
    %>
        
    <mt:if value="<%=test%>">
          
    <mt:out value="<%=outValue%>">
          这是mt:out>打印出的内容。
          
    </mt:out>
        
    </mt:if>

     
    <HR>  
     
    <mt:if value="false">
         
    <mt:out value="<%=outValue%>">
              这些内容会显示在客户端。
           
    </mt:out>
    </mt:if>

    <br>
     
    </BODY>  
    </HTML>
    运行:
  • 相关阅读:
    Binary Tree Inorder Traversal
    Populating Next Right Pointers in Each Node
    Minimum Depth of Binary Tree
    Majority Element
    Excel Sheet Column Number
    Reverse Bits
    Happy Number
    House Robber
    Remove Linked List Elements
    Contains Duplicate
  • 原文地址:https://www.cnblogs.com/eflylab/p/623163.html
Copyright © 2011-2022 走看看