2_Jsp标签_传统标签功能简介

1传统标签接口关系:                                  

2功能简介                                                  

3代码测试                                                  

  1.标签体内容控制 StartTag

package com.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

public class EvalBodyTag extends TagSupport {
    private boolean show;
    public void setShow(boolean show){
        this.show = show;
    
    }
    public int doStartTag() throws JspException {
        // TODO Auto-generated method stub
        if(show==true)
            return super.EVAL_BODY_INCLUDE;
        else
            return super.SKIP_BODY;
    }

}
EvalBodyTag

  2.标签结束后的处理  EndTag

package com.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

public class ShowPageTag extends TagSupport {
    private boolean show;
    public void setShow(boolean show){
        this.show = show;
    }
    @Override
    
    public int doEndTag() throws JspException {
        System.out.println(show);
        if(show==true)
            return super.EVAL_PAGE;
        else
            return super.SKIP_PAGE;
    }
    @Override
    public int doStartTag() throws JspException {
        return super.EVAL_BODY_INCLUDE;
    }

}
ShowPageTag

  3. 标签内容处理完后--doAfterBody

package com.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.IterationTag;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

public class RepeatTag extends TagSupport{
    
    private int count;
    public void setCount(int count){
        this.count = count;
    }
    public int doAfterBody() throws JspException {
        count--;
        if(count>0)
            return RepeatTag.EVAL_BODY_AGAIN;
        else
            return RepeatTag.SKIP_BODY;
    }

    public int doStartTag() throws JspException {
        return Tag.EVAL_BODY_INCLUDE;
    }
    
}
RepeatTag

 

    4. 内容格式化

package com.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class BodyContentTag extends BodyTagSupport {


    @Override
    public int doEndTag() throws JspException {
        BodyContent body = this.getBodyContent();
        String bc = body.getString();
        bc = bc.toUpperCase();
        try {
            this.pageContext.getOut().write(bc);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return super.doStartTag();
    
    }

    @Override
    public int doStartTag() throws JspException {
        return super.EVAL_BODY_BUFFERED;
    }

}
BodyContentTag

 

4 tld文件约束:                                        

<?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://self-tag-with-hello</uri>
    
    <tag>
        <name>helloWorld</name>

        <tag-class>com.tag.HelloWorldTag</tag-class>
        <body-content>empty</body-content>
          <attribute> 
                <name>name</name> 
                <required>true</required> 
                <rtexprvalue>true</rtexprvalue> 
          </attribute> 
    </tag>
    
    <tag>
        <name>evalbody</name>
        <tag-class>com.tag.EvalBodyTag</tag-class>
        <body-content>JSP</body-content>
        <attribute>
            <name>show</name>
            <required>true</required>
        </attribute>
         
    </tag>
    
    <tag>
        <name>showpage</name>
        <tag-class>com.tag.ShowPageTag</tag-class>
        <body-content>JSP</body-content>
        <attribute>
            <name>show</name>
            <required>true</required>
        </attribute>
         
    </tag>
    
      <tag>
        <name>repeatbody</name>
        <tag-class>com.tag.RepeatTag</tag-class>
        <body-content>JSP</body-content>
        <attribute>
            <name>count</name>
            <required>true</required>
        </attribute>
         
    </tag>
      <tag>
        <name>changeContent</name>
        <tag-class>com.tag.BodyContentTag</tag-class>
        <body-content>JSP</body-content>
    
    </tag>
    
</taglib>  
MyTag

 

5页面结果展示                                           

 

 

 

2_Jsp标签_传统标签功能简介,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。