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>