自定义标签实现switch属性

switch的实现考虑因素有很多,比else if还复杂  原理其实很简单   就是反复调用case  判断是否终止循环

switch类

package class3g.web.tag;

import java.io.IOException;

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

public class MySwitchTag extends SimpleTagSupport {
    private int exp;
    
    private boolean lastCase_break = false;  //上一个标签的break状态
    private boolean lastCase_done = false;  //上一个标签的执行状态
    
    private boolean terminate = false;
    

    public boolean isLastCase_break() {
        return lastCase_break;
    }

    public void setLastCase_break(boolean lastCase_break) {
        this.lastCase_break = lastCase_break;
    }

    public boolean isLastCase_done() {
        return lastCase_done;
    }

    public void setLastCase_done(boolean lastCase_done) {
        this.lastCase_done = lastCase_done;
    }

    public boolean isTerminate() {
        return terminate;
    }

    public void setTerminate(boolean terminate) {
        this.terminate = terminate;
    }

    public int getExp() {
        return exp;
    }

    public void setExp(int exp) {
        this.exp = exp;
    }


    @Override
    public void doTag() throws JspException, IOException {
        this.getJspBody().invoke(null);
    }


}

case类

package class3g.web.tag;

import java.io.IOException;

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

public class MyCaseTag extends SimpleTagSupport {
    private int value;
    private boolean breakSwitch;

    public void setValue(int value) {
        this.value = value;
    }

    public void setBreakSwitch(boolean breakSwitch) {
        this.breakSwitch = breakSwitch;
    }

    @Override
    public void doTag() throws JspException, IOException {
        MySwitchTag parent = (MySwitchTag) this.getParent();

        if (parent.isTerminate() == false) {
            if (parent.isLastCase_done() == false) {
                if (parent.getExp() == value) {
                    this.getJspBody().invoke(null);
                    parent.setLastCase_done(true);

                    parent.setTerminate(breakSwitch);
                }
            } else {
                if (parent.isLastCase_break() == false) {
                    this.getJspBody().invoke(null);
                    parent.setLastCase_done(true);
                    parent.setLastCase_break(breakSwitch);

                    parent.setTerminate(breakSwitch);

                }
            }
        }
    }

}

default类

package class3g.web.tag;

import java.io.IOException;

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

public class MyDefaultTag extends SimpleTagSupport {

    @Override
    public void doTag() throws JspException, IOException {
        MySwitchTag parent =  (MySwitchTag) this.getParent();
        if(parent.isTerminate() == false){
            this.getJspBody().invoke(null);
            parent.setTerminate(true);
        }
    }

}

JSP页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.class3g.com" prefix="g" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
 
  <body>

    <g:switch exp="2">
        <g:case value="1" breakSwitch="true" >
            11111111111<br/>
        </g:case>
        <g:case value="2" breakSwitch="false" >
            22222222222222<br/>
        </g:case>
        <g:case value="3" breakSwitch="true" >
            333333333333333<br/>
        </g:case>
        <g:default>
            dddddddddddddddddd<br/>
        </g:default>    
    </g:switch>
    <!--
        switch:带有一个exp属性,三个成员变量
            terminate作为中断执行break的标志
            lastCase_Done:上一个case标签的执行状态
            lastCase_break:上一个case标签的break值状态            
            
        case:两个属性,value和布尔型的breakSwitch,没成员
        default:没属性,没成员
     -->
  </body>
</html>

g,tld:描述文件

<?xml version="1.0" encoding="UTF-8" ?>

<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>There are my custom tags.</description>
    <tlib-version>1.0</tlib-version>
    <short-name>g</short-name>
    <uri>http://www.class3g.com</uri>
    
    
    <tag>
        <description>switch tag</description>
        <name>switch</name>
        <tag-class>class3g.web.tag.MySwitchTag</tag-class>
        <body-content>scriptless</body-content>
        <attribute>
            <name>exp</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
    
    
    <tag>
        <description>case tag </description>
        <name>case</name>
        <tag-class>class3g.web.tag.MyCaseTag</tag-class>
        <body-content>scriptless</body-content>
        <attribute>
            <name>value</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>breakSwitch</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
    
    <tag>
        <description>default </description>
        <name>default</name>
        <tag-class>class3g.web.tag.MyDefaultTag</tag-class>
        <body-content>scriptless</body-content>
    </tag>

</taglib>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值