gpt4 book ai didi

jsp - 避免 JSP 中的代码重复

转载 作者:行者123 更新时间:2023-12-01 23:20:32 24 4
gpt4 key购买 nike

我正在使用 JSP 和 Spring 框架。在一个页面中,我希望在相应的 select 元素旁边有五个按钮。我的问题是如何避免为此重复代码?

这是我的一个按钮选择组合的代码:

       <tr>
<td>
<sf:label path="inputFile">Select a file:</sf:label>
</td>
<td>
<sf:select path="inputFile" cssStyle="width: 250px;" onchange="enableLocal(this.selectedIndex,'matButton')">
<sf:option value="Upload a Local File" />
<sf:option value=" --- Available already: --- " disabled="true" />
<sf:options items="${flowData.availableInputs}" />
</sf:select>
</td>
<td>
<input id="matButton" type="file" name="inputFile"/>
</td>
</tr>

在上面的代码中,名称inputFilematButton 将被视为参数,即其他按钮将具有不同的名称。

我想到的是一个带有两个参数的函数,可以生成上述代码。可能吗?

最佳答案

创建一个 custom JSP tag file .

/WEB-INF/tags/file.tag

<%@ tag body-content="empty" %>
<%@ attribute name="selectPath" required="true" type="java.lang.String" %>
<%@ attribute name="buttonId" required="true" type="java.lang.String" %>
<%@ taglib prefix="sf" uri="...???..." %>

<tr>
<td>
<sf:label path="${selectPath}">Select a file:</sf:label>
</td>
<td>
<sf:select path="${selectPath}" cssStyle="width: 250px;" onchange="enableLocal(this.selectedIndex,'${buttonId}')">
<sf:option value="Upload a Local File" />
<sf:option value=" --- Available already: --- " disabled="true" />
<sf:options items="${flowData.availableInputs}" />
</sf:select>
</td>
<td>
<input id="${buttonId}" type="file" name="inputFile"/>
</td>
</tr>

(我不知道那个 sf 标签库是什么,你必须自己完成它的 URI)

按如下方式使用它,其中 file.tag 文件的基本文件名:

<%@ taglib prefix="my" tagdir="/WEB-INF/tags" %>
...
<my:file selectPath="inputFile" buttonId="matButton" />

关于jsp - 避免 JSP 中的代码重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12514062/

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