1.控制JSP页面某一部分内容是否执行
public void doTag()
this.getJspBody().invoke(null);执行
空白,不执行
2.控制JSP页面内容重复执行
1 package cn.itcast.web.simpletag; 2 3 import java.io.IOException; 4 5 import javax.servlet.jsp.JspException; 6 import javax.servlet.jsp.tagext.JspFragment; 7 import javax.servlet.jsp.tagext.SimpleTagSupport; 8 9 public class SimpleTagDemo2 extends SimpleTagSupport { 10 11 @Override 12 public void doTag() throws JspException, IOException { 13 // TODO Auto-generated method stub 14 JspFragment jf = this.getJspBody(); 15 16 for (int i = 0; i < 10; i++) { 17 jf.invoke(null); 18 } 19 } 20 21 }
3.修改JSP页面内容输出
1 package cn.itcast.web.simpletag; 2 3 import java.io.IOException; 4 import java.io.StringWriter; 5 6 import javax.servlet.jsp.JspContext; 7 import javax.servlet.jsp.JspException; 8 import javax.servlet.jsp.tagext.JspFragment; 9 import javax.servlet.jsp.tagext.SimpleTagSupport; 10 11 public class SimpleTagDemo3 extends SimpleTagSupport { 12 13 @Override 14 public void doTag() throws JspException, IOException { 15 // TODO Auto-generated method stub 16 17 JspFragment jf = this.getJspBody(); 18 StringWriter sw = new StringWriter(); 19 jf.invoke(sw); 20 21 String content = sw.getBuffer().toString(); 22 23 content = content.toUpperCase(); 24 25 JspContext pageContent = this.getJspContext(); 26 pageContent.getOut().write(content); 27 } 28 29 }
4.控制标签余下的JSP不执行
1 package cn.itcast.web.simpletag; 2 3 import java.io.IOException; 4 5 import javax.servlet.jsp.JspException; 6 import javax.servlet.jsp.SkipPageException; 7 import javax.servlet.jsp.tagext.SimpleTagSupport; 8 9 public class SimpleTagDemo4 extends SimpleTagSupport { 10 11 @Override 12 public void doTag() throws JspException, IOException { 13 // TODO Auto-generated method stub 14 throw new SkipPageException(); 15 } 16 17 }
开发带属性的标签
要想让一个自定义标签具有属性,通常需要完成两个任务:
在标签处理器中编写每个属性对应的setter方法
1 package cn.itcast.web.simpletag; 2 3 import java.io.IOException; 4 5 import javax.servlet.jsp.JspException; 6 import javax.servlet.jsp.tagext.SimpleTagSupport; 7 8 public class SimpleTagDemo5 extends SimpleTagSupport { 9 10 private int count; 11 12 public void setCount(int count) { 13 this.count = count; 14 } 15 16 @Override 17 public void doTag() throws JspException, IOException { 18 // TODO Auto-generated method stub 19 20 for (int i = 0; i < count; i++) { 21 this.getJspBody().invoke(null); 22 } 23 } 24 25 }
在tld文件中描述标签的属性
1 <tag> 2 <name>demo5</name> 3 <tag-class>cn.itcast.web.simpletag.SimpleTagDemo5</tag-class> 4 <body-content>scriptless</body-content> 5 <attribute> 6 <name>count</name> 7 <required>true</required> 8 <rtexprvalue>true</rtexprvalue> 9 </attribute> 10 </tag>
wewe