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   疯狂的妞妞  阅读(314)  评论(0编辑  收藏  举报

(评论功能已被禁用)
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示