1.完全的示例代码:要实现的功能是父标签中有name属性,子标签将父标签的name属性值打印到jsp页面上。
1.1 父类和子类的标签处理器类
testParentTag.java
package com.javaweb.tag;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.SimpleTagSupport;public class testParentTag extends SimpleTagSupport { private String name=\"大众koala\"大众; public String getName(){ return name; } @Override public void doTag() throws JspException,IOException{ System.out.println(\公众父标签name:\"大众+name); getJspBody().invoke(null); }}
SonTag.java
package com.javaweb.tag;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.JspTag;import javax.servlet.jsp.tagext.SimpleTagSupport;public class SonTag extends SimpleTagSupport { public void doTag() throws JspException,IOException { //1.得到父标签的引用 JspTag parent=getParent(); //2.获取父标签的name属性 //只有testParentTag才有name属性,以是须要将父标签的引用parent强转为testParentTag类 testParentTag parentTag=(testParentTag)parent; String name=parentTag.getName(); //3.把name值打印到jsp页面上 getJspContext().getOut().print(\"大众子标签输出name:\公众+name); }}
1.2 描述属性的tld文件,testParentTag.tld
<?xml version=\"大众1.0\"大众 encoding=\"大众UTF-8\公众?>
<taglib xmlns=\"大众http://java.sun.com/xml/ns/javaee\"大众
xmlns:xsi=\"大众http://www.w3.org/2001/XMLSchema-instance\公众
xsi:schemaLocation=\公众http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd\"大众
version=\"大众2.1\"大众>
<description>MyTag 1.1 core library</description>
<display-name>MyTag core</display-name>
<tlib-version>1.1</tlib-version>
<short-name>c</short-name>
<uri>http://java.koalatest.com/jsp/jstl/core</uri>
<tag>
<name>ParentTag</name>
<tag-class>com.javaweb.tag.testParentTag</tag-class>
<body-content>scriptless</body-content>
</tag>
<tag>
<name>SonTag</name>
<tag-class>com.javaweb.tag.SonTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
1.3 jsp调用文件,Parent.jsp
<%@ page language=\公众java\公众 import=\"大众java.util.\"大众 pageEncoding=\"大众UTF-8\"大众%><%@ taglib uri=\公众http://java.koalatest.com/jsp/jstl/core\公众 prefix=\公众koala\"大众%><%String path = request.getContextPath();String basePath = request.getScheme()+\"大众://\"大众+request.getServerName()+\公众:\"大众+request.getServerPort()+path+\"大众/\"大众;%><!DOCTYPE HTML PUBLIC \"大众-//W3C//DTD HTML 4.01 Transitional//EN\"大众><html> <head> <base href=\"大众<%=basePath%>\"大众> <title>My JSP 'Parent.jsp' starting page</title> <meta http-equiv=\"大众pragma\"大众 content=\公众no-cache\公众> <meta http-equiv=\公众cache-control\公众 content=\公众no-cache\公众> <meta http-equiv=\"大众expires\"大众 content=\"大众0\"大众> <meta http-equiv=\"大众keywords\"大众 content=\"大众keyword1,keyword2,keyword3\"大众> <meta http-equiv=\公众description\"大众 content=\"大众This is my page\"大众> <!-- <link rel=\"大众stylesheet\公众 type=\"大众text/css\"大众 href=\"大众styles.css\"大众> --> </head> <body> <!-- 父标签打印name到掌握台 --> <koala:ParentTag> <!-- 子标签以父标签的标签体存在,子标签把父标签的name属性打印到jsp页面上 --> <koala:SonTag/> </koala:ParentTag> </body></html>
运行后输出结果:
2.开拓有父标签的标签
2.1 父标签无法获取子标签的引用,父标签仅把子标签作为标签体来利用,在jsp页面的调用可以看出。
<body> <koala:ParentTag> <koala:SonTag/> </koala:ParentTag></body>
2.2 子标签可以通过getParent()方法来获取父标签的引用(须要继续SimpleTagSupport或实现SimpleTag接口的方法):若子标签的确有父标签,jsp引擎会把代表父标签的引用通过setParent(JspTag parent)赋给标签处理器。
2.3 把稳:父标签的类型是JspTag类型,该接口是一个空接口,是用来统一SimplleTag和Tag的,实际利用须要进行类型的逼迫转换。
2.4 在tld配置文件中,无需为父标签有额外的配置,但子标签因此标签体的形式存在的,以是父标签的<body-content></body-content>需设置为scriptless。
3.自己开拓带有父标签choose的标签when和otherwise实现以下功能:
<c:choose> <c:when test=\"大众${param.age>22}\"大众>大学毕业</c:when> <c:when test=\"大众${param.age>18}\"大众>高中毕业</c:when> <c:otherwise>初中以下毕业</c:otherwise></c:choose>
3.1 需求实现思路
①开拓三个标签:choose,when,otherwise
②个中when标签有一个boolean类型的属性:test
③choose是when和otherwise的父标签,when在otherwise之前利用
④在父标签choose中定义一个全局的\公众boolean\公众类型的flag,用来判断字标签在知足条件的情形下是否实行.
若when的test为true,且choose的flag类型为true,则实行when的标签体(正常输出标签体的内容),同时把flag设置为false;
若when的test为true,且choose的flag类型为false,则不实行标签体;
若flag为true,otherwise实行标签体。
3.2 实当代码
标签处理器类:
chooseTag.java
package com.javaweb.tag;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.SimpleTagSupport;public class otherwiseTag extends SimpleTagSupport { public void doTag() throws JspException,IOException { chooseTag choosetag=(chooseTag)getParent(); if (choosetag.isFlag()){ getJspBody().invoke(null); } }}
whenTag.java
package com.javaweb.tag;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.SimpleTagSupport;public class whenTag extends SimpleTagSupport { private boolean test; public void setTest(boolean test){ this.test=test; } public whenTag() throws JspException,IOException { if(test){ chooseTag choosetag=(chooseTag)getParent(); boolean flag=choosetag.isFlag(); if(flag){ //用于把代表标签体的JspFragment工具通报给标签处理器工具 getJspBody().invoke(null); choosetag.setFlag(false); } } }}
otherwiseTag.java
package com.javaweb.tag;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.SimpleTagSupport;public class otherwiseTag extends SimpleTagSupport { public void doTag() throws JspException,IOException { chooseTag choosetag=(chooseTag)getParent(); if (choosetag.isFlag()){ getJspBody().invoke(null); } } }
描述属性的tld文件:
testParentTag.tld
<tag> <name>choose</name> <tag-class>com.javaweb.tag.chooseTag</tag-class> <body-content>scriptless</body-content> </tag> <tag> <name>when</name> <tag-class>com.javaweb.tag.whenTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>test</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>otherwise</name> <tag-class>com.javaweb.tag.otherwise</tag-class> <body-content>scriptless</body-content> </tag>
jsp调用文件
<koala:choose> <koala:when test=\公众${param.age>22}\公众>大学毕业</koala:when> <koala:when test=\"大众${param.age>18}\公众>高中毕业</koala:when> <koala:otherwise>初中以下毕业</koala:otherwise></koala:choose>