<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.2 core librarydescription>
<display-name>MyTag coredisplay-name>
<tlib-version>1.2tlib-version>
<short-name>xsshort-name>
<uri>http://xs.com/jsp/MyTag/coreuri>
<tag>
<name>parentTagname>
<tag-class>com.xs.javaweb.ParentTagtag-class>
<body-content>scriptlessbody-content>
tag>
<tag>
<name>sonTagname>
<tag-class>com.xs.javaweb.SonTagtag-class>
<body-content>emptybody-content>
tag>
taglib>
ParentTag.java
/**
* All rights Reserved,Designed By XS
* @Title: ParentTag.java
* @Package com.xs.javaweb
* @Description: TODO
* @author: XS
* @date: 2019年3月14日 下午3:55:02
* @version V1.0
*/
package com.xs.javaweb;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
/**
* @ClassName: ParentTag
* @Description: TODO
* @author: XS
* @date: 2019年3月14日 下午3:55:02
* @version V1.0
*/
public class ParentTag extends SimpleTagSupport {
private String name = "XS";
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* Title: doTag
* Description:
* @see javax.servlet.jsp.tagext.SimpleTagSupport#doTag()
* @throws JspException
* @throws IOException
*/
@Override
public void doTag() throws JspException, IOException {
System.out.println("父标签处理器类 name:" + name);
getJspBody().invoke(null);
}
}
SonTag.java
/**
* All rights Reserved,Designed By XS
* @Title: SonTag.java
* @Package com.xs.javaweb
* @Description: TODO
* @author: XS
* @date: 2019年3月14日 下午3:56:35
* @version V1.0
*/
package com.xs.javaweb;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspTag;
import javax.servlet.jsp.tagext.SimpleTagSupport;
/**
* @ClassName: SonTag
* @Description: TODO
* @author: XS
* @date: 2019年3月14日 下午3:56:35
* @version V1.0
*/
public class SonTag extends SimpleTagSupport {
/**
* Title: doTag
* Description:
* @see javax.servlet.jsp.tagext.SimpleTagSupport#doTag()
* @throws JspException
* @throws IOException
*/
@Override
public void doTag() throws JspException, IOException {
//1.得到父标签的引用。
JspTag parent = getParent();
//2.获取父标签的name属性。
ParentTag parentTag = (ParentTag)parent;
String name = parentTag.getName();
//3.把 name 值打印到JSP页面上。
getJspContext().getOut().println(name);
}
}
test.jsp
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="com.xs.javaweb.Customer"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="xs" uri="http://xs.com/jsp/MyTag/core" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
<xs:parentTag>
<xs:sonTag/>
xs:parentTag>
body>
html>
<c:choose>
<c:when test="${param.age > 24}">大学毕业c:when>
<c:when test="${param.age > 20}">高中毕业c:when>
<c:otherwise>高中以下...c:otherwise>
c:choose>
> 开发 3 个标签:choose,when,otherwise
> 其中 when 标签有一个 boolean 类型的属性:test
> choose 是 when 和 otherwise 的父标签
> when 在 otherwise 之前使用
> 在父标签 choose 中定义一个 "全局" 的 boolean 类型的 flag:用于判断子标签在满足条件的情况下是否执行。
* 若 when 的 test 为 true, 且 when 的父标签的 flag 也为 true,则执行 when 的标签体(正常输出标签体的内容),同时把 flag 设置为 false。
* 若 when 的 test 为 true,且 when 的父标签的 flag 为 false,则不执行标签体。
* 若 flag 为 true,otherwise 执行标签体。
mytag.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.2 core librarydescription>
<display-name>MyTag coredisplay-name>
<tlib-version>1.2tlib-version>
<short-name>xsshort-name>
<uri>http://xs.com/jsp/MyTag/coreuri>
<tag>
<name>chooseTagname>
<tag-class>com.xs.javaweb.ChooseTagtag-class>
<body-content>scriptlessbody-content>
tag>
<tag>
<name>whenTagname>
<tag-class>com.xs.javaweb.WhenTagtag-class>
<body-content>scriptlessbody-content>
<attribute>
<name>testname>
<required>truerequired>
<rtexprvalue>truertexprvalue>
attribute>
tag>
<tag>
<name>otherwiseTagname>
<tag-class>com.xs.javaweb.OtherwiseTagtag-class>
<body-content>scriptlessbody-content>
tag>
taglib>
ChooseTag.java
/**
* All rights Reserved,Designed By XS
* @Title: ChooseTag.java
* @Package com.xs.javaweb
* @Description: TODO
* @author: XS
* @date: 2019年3月14日 下午4:19:26
* @version V1.0
*/
package com.xs.javaweb;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
/**
* @ClassName: ChooseTag
* @Description: TODO
* @author: XS
* @date: 2019年3月14日 下午4:19:26
* @version V1.0
*/
public class ChooseTag extends SimpleTagSupport {
private boolean flag = true;
/**
* @param flag: the flag to set
*/
public void setFlag(boolean flag) {
this.flag = flag;
}
/**
* @return the flag
*/
public boolean isFlag() {
return flag;
}
/**
* Title: doTag
* Description:
* @see javax.servlet.jsp.tagext.SimpleTagSupport#doTag()
* @throws JspException
* @throws IOException
*/
@Override
public void doTag() throws JspException, IOException {
getJspBody().invoke(null);
}
}
WhenTag.java
/**
* All rights Reserved,Designed By XS
* @Title: WhenTag.java
* @Package com.xs.javaweb
* @Description: TODO
* @author: XS
* @date: 2019年3月14日 下午4:19:54
* @version V1.0
*/
package com.xs.javaweb;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
/**
* @ClassName: WhenTag
* @Description: TODO
* @author: XS
* @date: 2019年3月14日 下午4:19:54
* @version V1.0
*/
public class WhenTag extends SimpleTagSupport {
private boolean test;
/**
* @param test: the test to set
*/
public void setTest(boolean test) {
this.test = test;
}
/**
* Title: doTag
* Description:
* @see javax.servlet.jsp.tagext.SimpleTagSupport#doTag()
* @throws JspException
* @throws IOException
*/
@Override
public void doTag() throws JspException, IOException {
if (test) {
ChooseTag chooseTag = (ChooseTag)getParent();
boolean flag = chooseTag.isFlag();
if (flag) {
getJspBody().invoke(null);
chooseTag.setFlag(false);
}
}
}
}
OtherwiseTag.java
/**
* All rights Reserved,Designed By XS
* @Title: OtherwiseTag.java
* @Package com.xs.javaweb
* @Description: TODO
* @author: XS
* @date: 2019年3月14日 下午4:20:26
* @version V1.0
*/
package com.xs.javaweb;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
/**
* @ClassName: OtherwiseTag
* @Description: TODO
* @author: XS
* @date: 2019年3月14日 下午4:20:26
* @version V1.0
*/
public class OtherwiseTag extends SimpleTagSupport {
/**
* Title: doTag
* Description:
* @see javax.servlet.jsp.tagext.SimpleTagSupport#doTag()
* @throws JspException
* @throws IOException
*/
@Override
public void doTag() throws JspException, IOException {
ChooseTag chooseTag = (ChooseTag)getParent();
if (chooseTag.isFlag()) {
getJspBody().invoke(null);
}
}
}
test.jsp
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="com.xs.javaweb.Customer"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="xs" uri="http://xs.com/jsp/MyTag/core"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
<xs:chooseTag>
<xs:whenTag test="${param.age > 24}">大学毕业xs:whenTag>
<xs:whenTag test="${param.age > 20}">高中毕业xs:whenTag>
<xs:otherwiseTag>高中以下...xs:otherwiseTag>
xs:chooseTag>
body>
html>