【自定义标签开发】08-标签案例-开发if..else标签

来源:互联网 发布:成都卧龙大数据 苏明亮 编辑:程序博客网 时间:2024/04/27 22:14
上一次我们开发了if标签,但是我们在框架中大部分使用的是if...else标签,我们接下来就模拟sun的C标签的if...else标签:
<c:choose>       <c:when test="...">             ......     </c:when><c:otherwise>             ......     </c:otherwise></c:choose>
开发一个我们自己的if...else标签,以此来了解sun自定义标签的内涵。

我们最终的效果是:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@taglib uri="/example" prefix="z" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title>Hello</title>  </head>    <body>       <z:choose>            <z:when test="${user!=null}">             欢迎您!(*^__^*)           </z:when><z:otherwise>             您没有登录!~~(>_<)~~          </z:otherwise>      </z:choose>  </body>  </html>

我们可以看到,when不执行的话,就要执行otherwise,这就意味着,when和otherwise都要知晓我们的test中的值的真假,所以when和otherwise就要共享一个test的变量。为了实现共享同一个变量的功能,我们的做法是,让if和otherwise的两个标签处理类都实现一个带有test变量的“父亲”---choose标签。

也就是,我们一共要开发3个标签(when/otherwise/choose)。

首先我们开发choose标签
创建一个标签处理器类:


编写其中的逻辑:
package org.zyg.web.exampleTag;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.SimpleTagSupport;public class ChooseTag extends SimpleTagSupport {private boolean isDo;public boolean isDo() { //get方法return isDo;}public void setDo(boolean isDo) { //set方法this.isDo = isDo;}//控制标签体执行@Overridepublic void doTag() throws JspException, IOException {this.getJspBody().invoke(null);}}

然后创建when标签处理器类:


编写其中逻辑:
package org.zyg.web.exampleTag;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;}@Overridepublic void doTag() throws JspException, IOException {//得到父标签ChooseTag parent=(ChooseTag) this.getParent();if(test && !parent.isDo()){//当test的值为真this.getJspBody().invoke(null);//因为父类的isDo变量默认值是false,修改isDo是为了给otherwise作参考parent.setDo(true);}}}

最后创建otherwise标签处理器类:


编写其中逻辑:
package org.zyg.web.exampleTag;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.SimpleTagSupport;public class OtherWiseTag extends SimpleTagSupport {@Overridepublic void doTag() throws JspException, IOException {ChooseTag parent=(ChooseTag)this.getParent();if(!parent.isDo()){//当when中的test为假的时候this.getJspBody().invoke(null);parent.setDo(true);}}}

最后我们在z.tld配置文件中注册这三个标签:
<tag>    <name>choose</name><!-- 标签名 -->    <tag-class>org.zyg.web.exampleTag.ChooseTag</tag-class>    <body-content>scriptless</body-content><!-- 有无标签体(单标签还是成对标签) --></tag><tag>    <name>when</name><!-- 标签名 -->    <tag-class>org.zyg.web.exampleTag.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>org.zyg.web.exampleTag.OtherWiseTag</tag-class>    <body-content>scriptless</body-content><!-- 有无标签体(单标签还是成对标签) --></tag>

我们重启Web应用,访问相应页面:

这里我们可以看到,当我们用户不存在的时候,显示的是otherwise中的值。我们将user注册到session中:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@taglib uri="/example" prefix="z" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title>Hello</title>  </head>    <body>     <%  session.setAttribute("user","zyg");   %>    <z:choose>        <z:when test="${user!=null}">         欢迎您!(*^__^*)       </z:when><z:otherwise>         您没有登录!~~(>_<)~~      </z:otherwise>    </z:choose>  </body>  </html>

重新访问,就显示了登录状态:

以上就是类似if...else标签的开发。

转载请注明出处:http://blog.csdn.net/acmman/article/details/51187408

0 0
原创粉丝点击