码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2014-07-09 15:07:00      阅读:304      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   java   color   

1传统标签接口关系:                                  

bubuko.com,布布扣

2功能简介                                                  

bubuko.com,布布扣

3代码测试                                                  

  1.标签体内容控制 StartTag

bubuko.com,布布扣
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

bubuko.com,布布扣
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

bubuko.com,布布扣
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. 内容格式化

bubuko.com,布布扣
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文件约束:                                        

bubuko.com,布布扣
<?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页面结果展示                                           

bubuko.com,布布扣

 

 

 

2_Jsp标签_传统标签功能简介,布布扣,bubuko.com

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

标签:des   style   blog   http   java   color   

原文地址:http://www.cnblogs.com/foreverzd/p/3832302.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!