自定义标签实现Switch 20111114
来源:互联网 发布:6u的网络机柜价格 编辑:程序博客网 时间:2024/05/17 07:28
Switch的实现与If(){...}else if(){...}else{...}差不多,但又有区别,Switch语句中的逻辑比较多且有些乱,至少我是这样认为的。
在Switch中,有多个Case语句,有一个default,还有break语句,若case语句后没有break语句,则后面的条件无需匹配,直接执行。直到遇到break或default语句,用语言描述很容易理解,但用代码实现还是有些困难的。下面就是实现Switch功能的代码:
Demo.java
package class3g.tag.demo;
public class Demo {
public static void main(String[] args) {
switch(2){
case 1:
System.out.println("111111111");
break;
case 2:
System.out.println("222222222");
case 3:
System.out.println("333333333");
break;
default:
System.out.println("ddddddddd");
}
}
}
MySwitchTag.java
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);
}
}
MyCaseTag.java
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);
}
}
}
}
}
MyDefaultTag.java
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);
}
}
}
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/j2eehttp://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>
index.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>
当执行case2时,输入为22222222222222
333333333333333
- 自定义标签实现Switch 20111114
- 自定义标签实现switch属性
- jsp自定义标签实现java的switch……case实例
- Jsp自定义标签实现
- jsp实现自定义标签
- java实现自定义标签
- 实现jsp 自定义标签
- 自定义分页标签实现
- Alice实现自定义标签
- spring自定义标签实现
- spring自定义标签实现
- 如何实现自定义标签
- 实现jsp自定义标签
- 实现自定义select标签
- spring自定义标签实现
- Freemarker 自定义标签实现
- 实现自定义标签
- Android 自定义实现switch开关按钮
- 在Spring框架下获取Bean的方式总结
- 【毕业生】资历与跳槽
- JAVA1.5新特性----注解Annotation
- 浮想
- ipcs 查看IPC 对象信息
- 自定义标签实现Switch 20111114
- Java WEB开发--国际化
- 如何在cpanel上建立自己的网站之:mysql数据库的设置
- ListView嵌套在TabActivity里数据不更新解决办法
- [X86] 关于/usr下的ccs,df,sfw,ucb,openwin,xpg4,xpg6的含义
- Unity3D 游戏引擎之平面小球重力感应详解(十三)
- 利用油槽实现进程之间的通信
- CSS兼容的一些解决方法
- c# 委托的用法