• javaWeb 使用jsp开发 if else 标签


    1.jsp页面调用代码

    <t:choose>
        <t:when test="${user==null }">还没有登录</t:when>
        <t:otherwise>欢迎您: ${user }</t:otherwise>
    </t:choose>

    2.tld文件代码

        <tag>
            <name>choose</name>
            <tag-class>de.bvb.web.tag.ChooseTag</tag-class>
            <body-content>scriptless</body-content>
        </tag>
        <tag>
            <name>when</name>
            <tag-class>de.bvb.web.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>de.bvb.web.tag.OtherWiseTag</tag-class>
            <body-content>scriptless</body-content>
        </tag>

    3.标签实现类代码

    3.1 父标签代码

    package de.bvb.web.tag;
    
    import java.io.IOException;
    
    import javax.servlet.jsp.JspException;
    import javax.servlet.jsp.tagext.SimpleTagSupport;
    
    public class ChooseTag extends SimpleTagSupport {
        private boolean executed;
    
        public boolean isExecuted() {
            return executed;
        }
    
        public void setExecuted(boolean executed) {
            this.executed = executed;
        }
    
        @Override
        public void doTag() throws JspException, IOException {
            this.getJspBody().invoke(null);
        }
    }

    3.2 if 标签代码

    package de.bvb.web.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;
        }
    
        @Override
        public void doTag() throws JspException, IOException {
            ChooseTag parent = (ChooseTag) this.getParent();
            if (!parent.isExecuted() && test) {
                this.getJspBody().invoke(null);
                parent.setExecuted(true);
            }
    
        }
    
    }

    3.3 else 标签代码

    package de.bvb.web.tag;
    
    import java.io.IOException;
    
    import javax.servlet.jsp.JspException;
    import javax.servlet.jsp.tagext.SimpleTagSupport;
    
    public class OtherWiseTag extends SimpleTagSupport {
        @Override
        public void doTag() throws JspException, IOException {
            ChooseTag parent = (ChooseTag) this.getParent();
            if (!parent.isExecuted()) {
                this.getJspBody().invoke(null);
                parent.setExecuted(true);
            }
        }
    
    }
  • 相关阅读:
    解决html中&nbsp;在不同浏览器中占位大小不统一的问题 SUperman
    解决C#调用执行js报检索 COM 类工厂中 CLSID 为 {0E59F1D51FBE11D08FF200A0D10038BC} 组件失败
    面向对象程序设计_tesk1_寒假伊始
    面对对象程序设计_task2_1001.A+B Format (20)
    面向对象程序设计_Task5_Calculator1.5.0
    面向对象程序设计_Task4_Calculator1.1
    面向对象程序设计_课堂作业_01_Circle
    面向对象程序设计__Task3_Calculator
    面对对象程序设计_task2_C++视频教程
    pkgconfig 简述
  • 原文地址:https://www.cnblogs.com/Westfalen/p/5978732.html
Copyright © 2020-2023  润新知