父标签:
1、父标签无法获取子标签的引用,父标签仅把子标签作为标签体来使用
2、子标签可以通过getParent来获取父标签的引用,如果子标签的确有父标签,jsp引擎会把代表父标签的引用通过setParent赋给标签处理器
mytld.tld
<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>MyTag 1.0 core librarydescription>
<display-name>MyTag coredisplay-name>
<tlib-version>1.0tlib-version>
<short-name>saflyshort-name>
<uri>http://www.safly.com/mytag/coreuri>
<tag>
<name>parenTagname>
<tag-class>com.safly.ParentTagtag-class>
<body-content>scriptlessbody-content>
tag>
<tag>
<name>sonTagname>
<tag-class>com.safly.SonTagtag-class>
<body-content>emptybody-content>
tag>
taglib>
ParentTag
package com.safly;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class ParentTag extends SimpleTagSupport {
private String name ="safly" ;
public String getName() {
return name;
}
@Override
public void doTag() throws JspException, IOException {
System.out.println("父标签的标签处理器类"+name);
getJspBody().invoke(null);
}
}
SonTag
package com.safly;
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{
@Override
public void doTag() throws JspException, IOException {
JspTag parent = getParent();
ParentTag parentTag = (ParentTag) parent;
String name = parentTag.getName();
getJspContext().getOut().print("子标签输出name:"+name);
}
}
mytag.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@taglib uri="http://www.safly.com/mytag/core" prefix="safly"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
<safly:parenTag>
<safly:sonTag/>
safly:parenTag>
<c:choose>
<c:when test="${param.age>24}">大学毕业c:when>
<c:when test="${param.age>20}">高中毕业c:when>
<c:otherwise>高中以下c:otherwise>
c:choose>
body>
html>
浏览器输入http://localhost:8080/day01/mytag.jsp
页面输出:子标签输出name:safly 高中以下
http://localhost:8080/day01/mytag.jsp?age=30
页面输出:子标签输出name:safly 大学毕业
大概说下逻辑:
在父类标签ParentTag中,定义了一个方法getName,在其doTag方法中,getJspContext().getOut().print(“子标签输出name:”+name);
意思是获取其标签体的内容,输出到浏览器,标签体就是
<safly:sonTag/>
标签体也就是其子标签SonTag,在子标签中通过getParent获取父标签对象,然后调用父标签的getName方法,然后输出到浏览器中,也就是输出safly
另外c:choose是标准的标签库,就相当于if else。。。
楼下的例子就是自定义一个选择标签样式
选择标签:
mytld.tld
<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>MyTag 1.0 core librarydescription>
<display-name>MyTag coredisplay-name>
<tlib-version>1.0tlib-version>
<short-name>saflyshort-name>
<uri>http://www.safly.com/mytag/coreuri>
<tag>
<name>choosename>
<tag-class>com.safly.ChooseTagtag-class>
<body-content>scriptlessbody-content>
tag>
<tag>
<name>whenname>
<tag-class>com.safly.WhenTagtag-class>
<body-content>scriptlessbody-content>
<attribute>
<name>testname>
<required>truerequired>
<rtexprvalue>truertexprvalue>
attribute>
tag>
<tag>
<name>otherwisename>
<tag-class>com.safly.OtherwiseTagtag-class>
<body-content>scriptlessbody-content>
tag>
taglib>
ChooseTag
package com.safly;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class ChooseTag extends SimpleTagSupport {
private boolean flag = true;
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
@Override
public void doTag() throws JspException, IOException {
getJspBody().invoke(null);
}
}
WhenTag
package com.safly;
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;
}
@Override
public void doTag() throws JspException, IOException {
if (test) {
com.safly.ChooseTag chooseTag = (com.safly.ChooseTag) getParent();
boolean flag = chooseTag.isFlag();
if (flag) {
getJspBody().invoke(null);
chooseTag.setFlag(false);
}
}
}
}
OtherwiseTag
package com.safly;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import org.apache.taglibs.standard.tag.common.core.ChooseTag;
public class OtherwiseTag extends SimpleTagSupport {
@Override
public void doTag() throws JspException, IOException {
com.safly.ChooseTag chooseTag = (com.safly.ChooseTag) getParent();
if (chooseTag.isFlag()) {
getJspBody().invoke(null);
}
}
}
mytag.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@taglib uri="http://www.safly.com/mytag/core" prefix="safly"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
<safly:choose>
<safly:when test="${param.age>24}">大学毕业safly:when>
<safly:when test="${param.age>20}">高中毕业safly:when>
<safly:otherwise>高中以下safly:otherwise>
safly:choose>
body>
html>
http://localhost:8080/day01/mytag.jsp
高中以下
http://localhost:8080/day01/mytag.jsp?age=50
大学毕业
简单说下逻辑:
在ChooseTag类的doTag中
getJspBody().invoke(null);
直接输出子标签的内容的意思,另外还有一个flag布尔值初始化为true,
不过这布尔值没有在tld文件中,进行attribute配置,只是一个简单的方法而已
<tag>
<name>choosename>
<tag-class>com.safly.ChooseTagtag-class>
<body-content>scriptlessbody-content>
tag>
我们来看楼上的子标签WhenTag
<attribute>
<name>testname>
<required>truerequired>
<rtexprvalue>truertexprvalue>
attribute>
所以在子标签WhenTag中,布尔值test被赋值为true,在参数中 param.age>24大于24时候为大学毕业, {param.age>20}高中毕业,其他为高中以下
在子标签WhenTag中通过getParent获取父标签,因为父类标签ChooseTag中flag初始化为true,所以更加age的年龄大小,去输出相应的语句,一旦when标签条件成功,就chooseTag.setFlag(false);置为false,就不会执行后续的代码
如果我们将WhenTag中的doTag方法改成如下,也就是注释掉某行代码:
if (test) {
com.safly.ChooseTag chooseTag = (com.safly.ChooseTag) getParent();
boolean flag = chooseTag.isFlag();
if (flag) {
getJspBody().invoke(null);
// chooseTag.setFlag(false);
}
}
http://localhost:8080/day01/mytag.jsp?age=50
浏览器输入:大学毕业 高中毕业 高中以下