避免JSP中的代码重复

Mah*_*hdi 2 code-reuse jsp code-duplication

我正在使用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>
Run Code Online (Sandbox Code Playgroud)

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

我想到的是类似于具有两个参数的函数,它们将生成上述代码.可能吗?

Bal*_*usC 5

创建自定义JSP标记文件.

/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>
Run Code Online (Sandbox Code Playgroud)

(我不知道那个sftaglib是什么,你自己要完成它的URI)

使用如下,其中file是文件的基本文件名.tag:

<%@ taglib prefix="my" tagdir="/WEB-INF/tags" %>
...
<my:file selectPath="inputFile" buttonId="matButton" />
Run Code Online (Sandbox Code Playgroud)