JSP自定义标签(二)标签嵌套

标签嵌套的关键代码是Super.getParent(),子标签获取父级标签的实例之后,即可操作父级标签中的方法,以下是JSTL库中的Choose标签的功能实现:

 

Choose标签(父标签):

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 * 
 * @author ChenSS on 2017年6月18日
 */
public class Choose extends SimpleTagSupport {
    private boolean isDo;

    public boolean isDo() {
        return isDo;
    }

    public void setDo(boolean isDo) {
        this.isDo = isDo;
    }

    @Override
    public void doTag() throws JspException, IOException {
        // 原样输出标签内容
        getJspBody().invoke(null);
    }
}

When和OtherWith标(子标签):

 

关键代码就是获取父级标签中的属性

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspTag;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 * 
 * @author ChenSS on 2017年6月18日
 */
public class When extends SimpleTagSupport {
    private boolean test;

    public void setTest(boolean test) {
        this.test = test;
    }

    public void doTag() throws JspException, IOException {
        // 获取父级标签
        JspTag tag = super.getParent();
        if (tag instanceof Choose) {
            Choose parent = (Choose) tag;
            if (test && !parent.isDo()) {
                getJspBody().invoke(null);
                parent.setDo(true);
            }
        }
    }
}
import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspTag;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 * 
 * @author ChenSS on 2017年6月18日
 */
public class OtherWith extends SimpleTagSupport {
    @Override
    public void doTag() throws JspException, IOException {
        // 获取父级标签
        JspTag tag = super.getParent();
        if (tag instanceof Choose) {
            Choose parent = (Choose) tag;
            if (!parent.isDo()) {
                getJspBody().invoke(null);
                parent.setDo(true);
            }
        }
    }
}

.tld配置文件

 

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
    <tlib-version>1.0</tlib-version>
    <jsp-version>1.1</jsp-version>
    <short-name>inner</short-name>
    <uri>/css_inner_tag</uri>
    <!-- Choose标签 -->
    <tag>
        <name>choose</name>
        <tag-class>com.yt.tag.inner.Choose</tag-class>
        <body-content>scriptLess</body-content>
    </tag>
    <!-- When标签 -->
    <tag>
        <name>when</name>
        <tag-class>com.yt.tag.inner.When</tag-class>
        <body-content>scriptLess</body-content>
        <attribute>
            <name>test</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
    <!-- OrtherWith标签 -->
    <tag>
        <name>otherWith</name>
        <tag-class>com.yt.tag.inner.OtherWith</tag-class>
        <body-content>scriptLess</body-content>
    </tag>
</taglib> 

 

JSP页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!-- 引入自定义标签 -->
<%@ taglib uri="/css_inner_tag" prefix="tag"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
    <h1>测试标签嵌套属性</h1>
    <%request.setAttribute("count",5); %> 
    <tag:choose>
        <tag:when test="${requestScope.count>3}">大于3</tag:when>
        <tag:when test="${requestScope.count==3}">等于3</tag:when>
        <tag:otherWith>3</tag:otherWith>
    </tag:choose>
</body>
</html>

 

posted on 2017-06-18 14:13  疯狂的妞妞  阅读(322)  评论(0)    收藏  举报

导航