rtexprvalue

其实以前也有写过自定义标签, 但是没有注意到过<rtexprvalue>的用法, 最近这几天又用上自定义标签了, 突然发现<rtexprvalue>的用法是有讲究的.

rtexprvalue的全称是 Run-time Expression Value, 它用于表示是否可以使用JSP表达式.

当在<attribute>标签里指定<rtexprvalue>true</rtexprvalue>时, 表示该自定义标签的某属性的值可以直接指定或者通过动态计算指定, example as follow:

<sql:query var="result" >
select * from mytable order by nameid
</sql:query>
<%request.setAttribute("nameid", "2"); %>
<myTag:cupSize cupSize="1" cupSizes="${result}"></myTag:cupSize>
<myTag:cupSize cupSize="${nameid}" cupSizes="${result}"></myTag:cupSize>


当在<attribute>标签里指定<rtexprvalue>false</rtexprvalue>时, 表示该自定义标签的某属性的值只能直接指定, example as follow:
<myTag:cupSize cupSize="1" cupSizes="${result}"></myTag:cupSize>
### 创建 JSP 自定义标签来计算梯形面积 为了创建一个用于计算梯形面积的 JSP 自定义标签,需要遵循以下结构: #### 1. 定义 TLD 文件 首先,在 `WEB-INF` 目录下创建一个新的 `.tld` 文件,命名为 `trapezoid.tld`。 ```xml <?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/dtd/web-jsptaglibrary_2_0.dtd" version="2.0"> <tlib-version>1.0</tlib-version> <short-name>TrapezoidTagLib</short-name> <uri>/tags/trapezoid</uri> <!-- Define the custom tag --> <tag> <name>areaOfTrapezoid</name> <tag-class>com.example.TrapezoidTagHandler</tag-class> <body-content>empty</body-content> <attribute> <name>baseOne</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>baseTwo</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>height</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib> ``` #### 2. 编写 Tag Handler 类 接着,实现处理逻辑并将其封装到 Java 类中。这个类负责接收参数并通过公式 `(b1+b2)*h/2` 计算梯形面积。 ```java package com.example; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.SimpleTagSupport; import java.io.IOException; public class TrapezoidTagHandler extends SimpleTagSupport { private double baseOne; // First Base Length private double baseTwo; // Second Base Length private double height; // Height public void setBaseOne(double baseOne){ this.baseOne = baseOne; } public void setBaseTwo(double baseTwo){ this.baseTwo = baseTwo; } public void setHeight(double height){ this.height = height; } @Override public void doTag() throws IOException{ JspWriter out = getJspContext().getOut(); double area = (this.baseOne + this.baseTwo) * this.height / 2; out.print(area); } } ``` #### 3. 使用自定义标签 最后,在 JSP 页面中引入并应用此自定义标签库以及具体标签。 ```jsp <%@ taglib prefix="trap" uri="/tags/trapezoid"%> <html> <body> <p>The area of trapezoid with bases 5 and 7, and height 4 is:</p> <b><trap:areaOfTrapezoid baseOne="5" baseTwo="7" height="4"/></b> </body> </html> ``` 通过上述步骤,成功构建了一个简单的 JSP 自定义标签,能够方便地在网页上展示不同尺寸梯形对应的面积值[^1]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值