javaweb带父标签的自定义标签
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>