标签:
我们回顾一下jsp的Tag自定义标签技术<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@taglib uri="/simpleTag" prefix="zyg2" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>用简单标签控制是否执行标签体</title> </head> <body> <zyg2:heddin> Helloworld! </zyg2:heddin> </body> </html>
package org.zyg.web.simpletag; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.JspFragment; import javax.servlet.jsp.tagext.SimpleTagSupport; //控制标签体是否执行 public class HeddinSimpleTag extends SimpleTagSupport { @Override public void doTag() throws JspException, IOException { JspFragment jf=this.getJspBody();//拿到标签体对象 jf.invoke(this.getJspContext().getOut());//控制标签体执行:输出标签体 } }我们这里选择了先输出标签体。
<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>2.0</tlib-version> <short-name>zyg2</short-name> <uri>h/simpleTag</uri><!-- 标签绑定的uri,用于引入 --> <tag> <name>heddin</name><!-- 标签名 --> <tag-class>org.zyg.web.simpletag.HeddinSimpleTag</tag-class> <body-content>scriptless</body-content><!-- 有无标签体(单标签还是成对标签) --> </tag> </taglib>
package org.zyg.web.simpletag; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.JspFragment; import javax.servlet.jsp.tagext.SimpleTagSupport; //控制标签体是否执行 public class HeddinSimpleTag extends SimpleTagSupport { @Override public void doTag() throws JspException, IOException { JspFragment jf=this.getJspBody();//拿到标签体对象 } }
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@taglib uri="/simpleTag" prefix="zyg2" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>用简单标签循环标签体</title> </head> <body> <zyg2:repeat> 嘻嘻嘻,好犀利(*^__^*) <br/> </zyg2:repeat> </body> </html>
package org.zyg.web.simpletag; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.JspFragment; import javax.servlet.jsp.tagext.SimpleTagSupport; public class RepeatSimpleTag extends SimpleTagSupport { @Override public void doTag() throws JspException, IOException { JspFragment jf=this.getJspBody();//拿到标签体对象 for (int i = 0; i < 5; i++) { jf.invoke(this.getJspContext().getOut()); } } }
<tag> <name>repeat</name><!-- 标签名 --> <tag-class>org.zyg.web.simpletag.RepeatSimpleTag</tag-class> <body-content>scriptless</body-content><!-- 有无标签体(单标签还是成对标签) --> </tag>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@taglib uri="/simpleTag" prefix="zyg2" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>用简单标签循环标签体</title> </head> <body> <zyg2:upper> abcdefg <br/> </zyg2:upper> </body> </html>
package org.zyg.web.simpletag; import java.io.IOException; import java.io.StringWriter; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.JspFragment; import javax.servlet.jsp.tagext.SimpleTagSupport; //修改标签体 public class UpperSimpleTag extends SimpleTagSupport { @Override public void doTag() throws JspException, IOException { JspFragment jf=this.getJspBody();//拿到标签体对象 StringWriter sw=new StringWriter(); jf.invoke(sw);//执行一个带缓冲的Writer String content=sw.toString();//获得标签体的内容 content = content.toUpperCase();//将内容变为大写 this.getJspContext().getOut().write(content); } }
<tag> <name>upper</name><!-- 标签名 --> <tag-class>org.zyg.web.simpletag.UpperSimpleTag</tag-class> <body-content>scriptless</body-content><!-- 有无标签体(单标签还是成对标签) --> </tag>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@taglib uri="/simpleTag" prefix="zyg2" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>用简单标签控制jsp页面的显示</title> </head> <zyg2:heddinPage/> <body> Hello ! </body> </html>
package org.zyg.web.simpletag; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.SkipPageException; import javax.servlet.jsp.tagext.SimpleTagSupport; //控制标签余下的jsp不执行 public class HeddinPageSimpleTag extends SimpleTagSupport { @Override public void doTag() throws JspException, IOException { //只要这个异常抛给jsp,该标签下面的jsp代码就不会执行 throw new SkipPageException(); } }
<tag> <name>heddinPage</name><!-- 标签名 --> <tag-class>org.zyg.web.simpletag.HeddinPageSimpleTag</tag-class> <body-content>empty</body-content><!-- 有无标签体(单标签还是成对标签) --> </tag>
转载请注明出处:http://blog.csdn.net/acmman/article/details/51093287
标签:
原文地址:http://blog.csdn.net/acmman/article/details/51093287