开发<c:if><c:else>标签解析及其源代码

本文介绍了如何开发和解析JSP中的自定义条件标签<c:if>和<c:else>。通过创建ChooseTag、WhenTag和OtherWiseTag类实现逻辑判断,当条件满足时执行相应标签体。示例代码展示了如何在JSP页面中使用这些自定义标签。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

开发<c:if><c:else>标签解析及其源代码

if else条件判定:

<c:if>aaaa</c:if>       :IfTag.java

<c:else>bbb</c:else>    ElseTag.java

共享一个boolean变量

<c:choose> 为父标签写一个标签处理器ChooseTag.java

       <c:when test=””></c:when>   标签处理器WhenTag.java

       <c:otherwise></c:otherwise>    标签处理器OtherWise.java

</c:choose>

为父标签写一个标签处理器ChooseTag.java:

import java.io.IOException;

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.tagext.JspFragment;

import javax.servlet.jsp.tagext.SimpleTagSupport;

public class ChooseTag extends SimpleTagSupport {

       private boolean flag=false;

       public boolean isFlag() {

              return flag;

       }

       public void setFlag(boolean flag) {

              this.flag = flag;

       }

       @Override

       public void doTag() throws JspException, IOException {

              JspFragment jf=this.getJspBody();

              jf.invoke(null);

       }

}

标签处理器WhenTag.java:

import java.io.IOException;

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.tagext.SimpleTagSupport;

public class WhenTag extends SimpleTagSupport {

       private boolean test1;

       public void setTest1(boolean test1) {

              this.test1 = test1;

       }

       @Override

       public void doTag() throws JspException, IOException {

              //获取父标签对象

              ChooseTag parent=(ChooseTag)this.getParent();

              if(test1 && !parent.isFlag()){

                     //处理该标签体

                     this.getJspBody().invoke(null);

                     parent.setFlag(true);

              }

       }

}

标签处理器OtherWise.java:

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.isFlag()){

                     this.getJspBody().invoke(null);

                     //将父标签的标志设置为true

                     parent.isFlag();

              }

       }

}

标签:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%@ taglib uri="http://www.hbsi.edu.com" prefix="c"%>

<%

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 'TestIf.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">

  </head>

  <body>

  <c:choose>

       <c:when test1="${user!=null}">aaaaa</c:when>

       <c:otherwise>bbbbb</c:otherwise>

</c:choose>

  </body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值