BodyTagSupport小案例1

做了个简单的实验:写一个tag,将tag body中的内容打印成一个三角形

代码很简单就不赘述了,直接贴在下面,值得注意的是这个图(摘自李兴华JAVA开发实战经典)

在做的过程中遇到了如下问题:

1. getBodyContent()返回null

  原因:doStartTag()应该返回EVAL_BODY_BUFFERED而不是EVAL_BODY_INCLUDE,否则bodyContent在doAfterBody()中是不可见的

2. pageContext.getOut().println()无法向页面输出内容

  原因:bodyContent本身就是一个JspWriter,应该使用BodyTagSupport.getPreviousOut().println()进行输出

  关于这个问题,参考此页面 http://www.coderanch.com/t/175316/java-Web-Component-SCWCD/certification/BodyTag-getPreviousOut-getPageContext-getOut

  

代码如下,这个Tag有一个可选参数:size,表示三角形的大小

FunnyTriangleTag.java

public class FunnyTriangleTag extends BodyTagSupport {
    private static final int DEFAULT_SIZE = 2;
    private int size = -1;
    
    public int getSize() {
        return size;
    }
    
    public void setSize(int size) {
        this.size = size;
    }

    private int count = 0;
    
    @Override
    public int doStartTag() throws JspException {
        if (size < DEFAULT_SIZE) {
            count = DEFAULT_SIZE;
        } else {
            count = size;
        }
        return EVAL_BODY_BUFFERED;
    }
    
    @Override
    public int doAfterBody() throws JspException {
        if (count-- > 0) {
            try {
                String string = this.getBodyContent().getString();
                this.getPreviousOut().println("<p>"+string+"</p>");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return EVAL_BODY_AGAIN;
        } else {
            return SKIP_BODY;
        }
    }
    
    @Override
    public int doEndTag() throws JspException {
        size = -1;
        count = 0;
        return EVAL_PAGE;
    }

}

index.jsp

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<%@ taglib uri="http://tags.xxx.com/foo" prefix="foo" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Insert title here</title>
</head>
<body>
    <foo:funnyTriangle size="5">你好</foo:funnyTriangle>
    <foo:funnyTriangle>默认1</foo:funnyTriangle>
    <foo:funnyTriangle size="-1">默认2</foo:funnyTriangle>
</body>
</html>

foo.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>xxx's foo tags</description>
  <display-name>xxx foo tags</display-name>
  <tlib-version>1.0</tlib-version>
  <short-name>foo</short-name>
  <uri>http://tags.xxx.com/foo</uri>
  
   <tag>
           <description>Print the string in the tag body repeatedly to form a triangle shape.</description>
           <name>funnyTriangle</name>
           <tag-class>com.v1.ex118.FunnyTriangleTag</tag-class>
           <body-content>JSP</body-content>
           <attribute>
               <description>
                   The size of the triangle, no less than 2. If the size is less
                   than 2, a default-sized triangle will be displayed.
               </description>
               <name>size</name>
               <required>false</required>
               <rtexprvalue>true</rtexprvalue> <!-- support EL -->
               <type>java.lang.Integer</type>
           </attribute>
   </tag>
</taglib>
原文地址:https://www.cnblogs.com/qrlozte/p/3540396.html