SimpleTag 标签

2017-07-08  本文已影响5人  基本密码宋
由于传统标签使用三个标签接口来完成不同的功能,显得过于繁琐,不利于标签技术的推广, SUN公司为降低标签技术的学习难度,在JSP 2.0中定义了一个更为简单、便于编写和调用的SimpleTag接口来实现标签的功能。实现SimpleTag接口的标签通常称为简单标签。简单标签共定义了5个方法:

接下来会用simpleTag来实现上篇的功能 小个小Demo


public class Demo1 extends SimpleTagSupport {

    @Override
    public void doTag() throws JspException, IOException {
        //得到标签体
        JspFragment jf=this.getJspBody();
        //得到 pageContext  从而得到流  进而进行操作
        PageContext pageContext = (PageContext) this.getJspContext();
//      jf.invoke(pageContext.getOut()); // 将流输入给浏览器
        
        //上面的操作 可以简单的写成  意思就是写给浏览器 数据 
        //jf.invoke(null);
        
        //如果不输出 标签体给 浏览器 则  注释掉  jf.invoke(null);  表示不给浏览器输出  流  则不显示 标签体
    }
}

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@taglib uri="/simpleTld" prefix="simpleTld"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>用 simpleTag 控制界面中的 jsp代码是否执行</title>
</head>
<body>
        
        <simpleTld:demo1>
                看看执行吗
        </simpleTld:demo1>
        
        
</body>
</html>
/**
 * 控制 标签体执行 10次
 *
 */
public class Demo2 extends SimpleTagSupport {

    @Override
    public void doTag() throws JspException, IOException {
        JspFragment jf = this.getJspBody();
        for(int i=0;i<10;i++){
            jf.invoke(null);
        }
    }
}
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@taglib uri="/simpleTld" prefix="simpleTld"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>用 simpleTag 控制界面中的 jsp代码执行几遍</title>
</head>
<body>
        
        <simpleTld:demo2>
                看看执行几遍<br/>
        </simpleTld:demo2>
        
        
</body>
</html>
public class Demo3  extends SimpleTagSupport{
    
    @Override
    public void doTag() throws JspException, IOException {
        JspFragment jf = this.getJspBody();
        StringWriter sw = new StringWriter();
        jf.invoke(sw); //将数据写到了   StringWriter 流中
        
        String content = sw.getBuffer().toString(); //得到写入的内容
        
        content = content.toUpperCase(); //将内容进行改变
        
        PageContext pageContent = (PageContext) this.getJspContext();//重新写给 流 中
        pageContent.getOut().write(content);
    }
    
}
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@taglib uri="/simpleTld" prefix="simpleTld"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>用 simpleTag 控制界面中的  标签体中的数据 </title>
</head>
<body>
        
        <simpleTld:demo3>
                 xxxxxxxx 
        </simpleTld:demo3>
        
        
</body>
</html>
public class Demo4 extends SimpleTagSupport{
    @Override
    public void doTag() throws JspException, IOException {
        throw new SkipPageException();
    }
}
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@taglib uri="/simpleTld" prefix="simpleTld"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>用 simpleTag 控制标签余下的jsp是否执行 </title>
</head>
<body>
        看看上面的执行呢
        <simpleTld:demo4>
                 xxxxxxxx 
        </simpleTld:demo4>
        
        
         看看 下面的jsp是否执行呢?
        
</body>
</html>
public class Demo5 extends SimpleTagSupport {
    
    private int count;//次数
    public void setCount(int count) {
        this.count = count;
    }

    @Override
    public void doTag() throws JspException, IOException {
         for (int i = 0; i < count; i++) {
             this.getJspBody().invoke(null);
        }
    }
    
}
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@taglib uri="/simpleTld" prefix="simpleTld"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>用 simpleTag 动态控制执行标签体的次数 </title>
</head>
<body>
            <!-- count={... 里面可以写入} -->
        <simpleTld:demo5 count="5">
            看看是否执行了5次  <br/>
        </simpleTld:demo5>
        
</body>
</html>

public class Demo6 extends SimpleTagSupport{
    
    private Date date;
    
    public void setDate(Date date) {
        this.date = date;
    }

    @Override
    public void doTag() throws JspException, IOException {
        System.out.println(date);
    }
}
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@taglib uri="/simpleTld" prefix="simpleTld"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>用 simpleTag 标签属性 </title>
</head>
<body>
    
    <%
        Date date=new Date();
        request.setAttribute("date", date);
    %> 
     
     <simpleTld:demo6 date="${date}">
     </simpleTld:demo6>
        
</body>
</html>

最后贴出 .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>/simpleTld</uri>
    
     <tag>
        <name>demo1</name>  <!-- 为标签处理器类配一个标签名 -->
        <tag-class>jeno.com.sample.tld.Demo1</tag-class>
        <body-content>scriptless</body-content> <!-- 注意当用 simpleTag时  不能用 JSP 用 scriptless 或者 empty  -->
    </tag>
    
      <tag>
        <name>demo2</name>  <!-- 为标签处理器类配一个标签名 -->
        <tag-class>jeno.com.sample.tld.Demo2</tag-class>
        <body-content>scriptless</body-content> <!-- 注意当用 simpleTag时  不能用 JSP 用 scriptless 或者 empty  -->
    </tag>
    
     <tag>
        <name>demo3</name>  <!-- 为标签处理器类配一个标签名 -->
        <tag-class>jeno.com.sample.tld.Demo3</tag-class>
        <body-content>scriptless</body-content> <!-- 注意当用 simpleTag时  不能用 JSP 用 scriptless 或者 empty  -->
    </tag>
     
     <tag>
        <name>demo4</name>  <!-- 为标签处理器类配一个标签名 -->
        <tag-class>jeno.com.sample.tld.Demo4</tag-class>
        <body-content>scriptless</body-content> <!-- 注意当用 simpleTag时  不能用 JSP 用 scriptless 或者 empty  -->
    </tag>
     
      <tag>
        <name>demo5</name>  <!-- 为标签处理器类配一个标签名 -->
        <tag-class>jeno.com.sample.tld.Demo5</tag-class>
        <body-content>scriptless</body-content> <!-- 注意当用 simpleTag时  不能用 JSP 用 scriptless 或者 empty  -->
        <attribute>
            <name>count</name>
            <required>true</required><!-- jsp中调用的时候 是否必须设置这个属性  -->
            <rtexprvalue>true</rtexprvalue>  <!-- 指示属性的值是否可以为一个表达式  是否可以写入 {...} 类似这种东西-->
        </attribute>
    </tag>
     
      
      <tag>
        <name>demo6</name>  <!-- 为标签处理器类配一个标签名 -->
        <tag-class>jeno.com.sample.tld.Demo6</tag-class>
        <body-content>scriptless</body-content> <!-- 注意当用 simpleTag时  不能用 JSP 用 scriptless 或者 empty  -->
        <attribute>
            <name>date</name>
            <required>true</required><!-- jsp中调用的时候 是否必须设置这个属性  -->
            <rtexprvalue>true</rtexprvalue>  <!-- 指示属性的值是否可以为一个表达式  是否可以写入 {...} 类似这种东西-->
        </attribute>
    </tag>
     
</taglib>
上一篇 下一篇

猜你喜欢

热点阅读