自定义choose标签以及其中包含的两个坑,jsp页面使用el表达式,运行时使用rtexprvalue

  1. 需求描述
    实现一个类似于如下图的自定义标签在这里插入图片描述
  2. 三个标签处理器类
    -myChoose
    -myWhen
    -myOther
    在父类Mychoose中定义一个是否执行的标志位opreated。
    myWhen中有一个判断条件是否成立的标志satisfied。
    (1)如果satisfied=true且opreated=false,则执行myWhen,执行完之后将opreated置为true
    (2)如果如果satisfied=false,则不执行当前的myWhen
    (3)如果如果opreated=false,则执行myOther

MyChoose 标签处理器类

public class MyChoose extends SimpleTagSupport {
    public boolean isOperated=false;
    public boolean isOperated() {
        return isOperated;
    }
    public void setOperated(boolean operated) {
        isOperated = operated;
    }
    @Override
    public void doTag() throws JspException, IOException {
        System.out.println("this is myChoose doTag...");
        getJspBody().invoke(null);
    }
}

myWhen标签处理器类


public class MyWhen extends SimpleTagSupport {
   private boolean satisfied=true;
   public boolean isSatisfied() {
       return satisfied;
   }
   public void setSatisfied(boolean satisfied) {
       this.satisfied = satisfied;
   }
   @Override
   public void doTag() throws JspException, IOException {
       System.out.println("条件是否满足:"+ satisfied);
       if (satisfied) {
           MyChoose myChoose = (MyChoose) getParent();
           boolean operated = myChoose.isOperated();
           if (!operated) {
              getJspBody().invoke(null);
               myChoose.setOperated(true);
           }
       }
   }
}

myOther标签处理器类

public class MyOther extends SimpleTagSupport {
    @Override
    public void doTag() throws JspException, IOException {
        MyChoose myChoose=(MyChoose) getParent();
        boolean operated = myChoose.isOperated();
        if(operated==false ){
          getJspBody().invoke(null);
       }
    }
}
  1. 文件描述符
    在这里注意第一个坑:myWhen的属性中,rtexprvalue值一定要置为true,表示其可以接受el表达式,否则会报According to TLD or attribute directive in tag file, attribute [satisfied] does not accept any expressions 错误
 <tag>
        <name>myChoose</name>
        <tag-class>com.paul.tag.MyChoose</tag-class>
        <body-content>scriptless</body-content>
    </tag>

    <tag>
        <name>myWhen</name>
        <tag-class>com.paul.tag.MyWhen</tag-class>
        <body-content>scriptless</body-content>
        <attribute>
            <name>test</name>
            <required>true</required>
           <!--一定要置为true-->
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
    <tag>
        <name>myOther</name>
        <tag-class>com.paul.tag.MyOther</tag-class>
        <body-content>scriptless</body-content>
    </tag>
  1. 使用
    这里包含第二个坑:页面中使用el表达式
    <%@ page isELIgnored=“false”%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false"%>
<%@ taglib prefix="puhao" uri="puhao" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<puhao:myChoose>
    <puhao:myWhen satisfied="${param.score > 90}">优秀</puhao:myWhen>
    <puhao:myWhen satisfied="${param.score > 60}">及格</puhao:myWhen>
    <puhao:myOther>不及格</puhao:myOther>
</puhao:myChoose>
</body>
</html>

http://localhost:8080/tag/myChoose.jsp?score=100

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值