自定义标签,在类中控制该自定义标签是否显示:
import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.Tag; import javax.servlet.jsp.tagext.TagSupport; //控制标签体是否执行 public class TagDemo1 extends TagSupport { @Override public int doStartTag() throws JspException { return Tag.EVAL_BODY_INCLUDE;//控制自定义标签执行 //return Tag.SKIP_BODY; //控制自定义标签不执行 } }
为自定义标签配置tld ,代码:
<tag> <name>demo1</name> <tag-class>cn.itcast.web.tag.TagDemo1</tag-class> <body-content>JSP</body-content> </tag>
2.jsp页面使用自定义标签:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="/itcast" 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>