gpt4 book ai didi

jsp - JSTL 自定义标签

转载 作者:行者123 更新时间:2023-12-04 00:16:47 27 4
gpt4 key购买 nike

我将如何为具有 2 个属性的自定义标签编写(只是一个模板),让我使用 jSTL 标签逻辑输出一个 html 片段(一个 html 表),可以从我的 jsp 调用。

可以不写java类就可以做到这一点,这是我在所有示例中看到的。

我想要实现的是将 JSP 中重复的 JSTL 逻辑外部化到自定义标记中,然后在运行时使用属性将所需的动态值传递给标记。

谢谢,

最佳答案

不要使用小脚本!它们是一种不好的做法,它们会让业务逻辑泄漏到您的 View 层中。

您可以使用 JSTL 创建标签文件;这很简单。 This是一个很好的起点。

一个例子:

mytable.tag :

<%@ attribute name="cell1" required="true" type="java.lang.String" description="Text to use in the first cell." %>
<%@ attribute name="cell2" required="false" type="java.lang.String" description="Text to use in the second cell." %>

<table>
<tr>
<td id = "cell1">${cell1}</td>
<td id = "cell2">${cell2}</td>
</tr>
</table>

假设您的标签在 /WEB-INF/tags ,然后您可以像这样使用它:
<%@ taglib prefix="mystuff" tagdir="/WEB-INF/tags" %>

<mystuff:mytable cell1="hello" cell2="world" />

关于jsp - JSTL 自定义标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6870476/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com