什么都不说,直接上代码:
import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.PageContext; import javax.servlet.jsp.tagext.JspFragment; import javax.servlet.jsp.tagext.SimpleTagSupport; //控制标签体是否执行 public class SimpleTagDemo1 extends SimpleTagSupport { //简单标签使用这个方法完成所有业务逻辑 @Override public void doTag() throws JspException, IOException { //得到代表标签体的JspFragment JspFragment jf = this.getJspBody(); //PageContext pageContext = (PageContext) this.getJspContext();
//jf.invoke(pageContext.getOut()); jf.invoke(null); //这个方法等同于上面注释两行代码. } }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="/simpleitcast" prefix="itcast" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>用简单标签实现控制标签体是否执行</title> </head> <body> <itcast:demo1> xxxx </itcast:demo1> </body> </html>
<description>A tag library exercising SimpleTag handlers.</description> <tlib-version>1.0</tlib-version> <short-name>SimpleTagLibrary</short-name> <uri>/simpleitcast</uri> <tag> <name>demo1</name> <!-- 为标签处理器类配一个标签名 --> <tag-class>cn.itcast.web.simpletag.SimpleTagDemo1</tag-class> <body-content>scriptless</body-content> </tag>