简体   繁体   English

如何为下拉列表等编写通用 JSP 模板?

[英]How to write a generic JSP Template for Dropdowns etc.?

I want to create JSP templates, ie JSP files that contain eg a dropdown or input field, to keep the styling of the elements in one place.我想创建 JSP 模板,即包含例如下拉或输入字段的 JSP 文件,以将元素的样式保存在一个位置。

Now these templates should be used in different UseCases.现在这些模板应该在不同的用例中使用。 For this I would like to pass the corresponding attributes by parameter.为此,我想通过参数传递相应的属性。 My model is always in the requestScope.我的 model 始终在 requestScope 中。 Here I include a template:这里我包括一个模板:

    <jsp:include page="/WEB-INF/jspf/edit_element/select.jsp">
       <jsp:param name="field" value="country"/>
       <jsp:param name="selected" value="${model.selectedCountry}"/>
       <jsp:param name="itemList" value="${model.countries}"/>
    </jsp:include>

Here my model contains an array of strings (countries like USA,Mexico,..) and a selected country.在这里,我的 model 包含一系列字符串(美国、墨西哥等国家/地区)和一个选定的国家/地区。

Here is my dropdown template:这是我的下拉模板:

<div class="select-wrapper">
    <form:select path="${param.field}" cssClass="combobox">
        <%--    First element--%>
        <form:option value=""><fmt:message key="empty_choice"/></form:option>

        <c:forEach items="${param.itemList}" var="item" varStatus="rowCounter" >

            <%--            parse String[] --%>
            <c:if test="${fn:startsWith(item, '[')}">
                <c:set var="item">${fn:replace(item, "[", "")}</c:set>
            </c:if>
            <c:if test="${fn:endsWith(item, ']')}">
                <c:set var="item">${fn:replace(item, "]", "")}</c:set>
            </c:if>

            <%-- Choose if selected statement is needed--%>
            <c:choose>
                <c:when test="${item eq param.selected}">
                    <form:option value="${rowCounter.index}" selected="selected">${item}</form:option>
                </c:when>
                <c:otherwise>
                    <form:option value="${rowCounter.index}">${item}</form:option>
                </c:otherwise>
            </c:choose>

        </c:forEach>

    </form:select>
</div>

As you can see I have the problem that JSP always converts all parameters to a string.如您所见,我遇到的问题是 JSP 总是将所有参数转换为字符串。 I tried to parse this string back again afterwards.之后我试图再次解析这个字符串。 Thus, removing the parenthesis from the first and last item.因此,从第一项和最后一项中删除括号。 The problem is on the one hand that it is not very elegant and also complicated cases can occur, so that the parsing becomes a challenge.问题一方面是它不是很优雅,而且可能会出现复杂的情况,从而使解析成为一个挑战。 On the other hand the comparison <c:when test="${item eq param.selected}"> always returns false as result, although the strings are the same.另一方面,比较 <c:when test="${item eq param.selected}"> 总是返回 false 作为结果,尽管字符串是相同的。

Is there an elegant way to handle a generic Template?是否有一种优雅的方式来处理通用模板? I also tried to give the template a string as a parameter, which then determines which attribute of the model should be accessed.我还尝试给模板一个字符串作为参数,然后确定应该访问 model 的哪个属性。 Something like this (unfortunately doesn't work):像这样的东西(不幸的是不起作用):

 <jsp:include page="/WEB-INF/jspf/edit_element/select.jsp">
   <jsp:param name="field" value="country"/>
   <jsp:param name="selected" value="selectedCountries"/>
   <jsp:param name="itemList" value="countries"/>
</jsp:include>

In my template I would like to use the string and access my model:在我的模板中,我想使用字符串并访问我的 model:

<c:set var="attribute" value="${param.selected}" scope="request"/>
${model.attribute} => Here the passed string should specify which attribute to access.

Thanks for any answer!感谢您的任何回答!

I recommend to use JSP 2.0 tag files syntax.我建议使用 JSP 2.0 标记文件语法。 This syntax helps you to create a new tag very easily.此语法可帮助您非常轻松地创建新标签。

Create the tag file as /WEB-INF/tags/select.tag :创建标签文件为/WEB-INF/tags/select.tag

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ tag description="Your custom tag" %>
<%@ attribute name="field"  %>
<%@ attribute name="selected"  %>
<%@ attribute name="itemList" type="java.util.List" %>

<select name="${field}">
<c:forEach var="item" items="${itemList}" >
    <option>${item}</option>
</c:forEach>
</select>

In your template:在您的模板中:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>

<tags:select field="countries" itemList="${countries}" selected="${selectedCountries}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JSP自定义标记中使用类,样式等全局HTML属性? - How to use global HTML attributes like class, style, etc. in JSP custom tags? 如何在Spring的JSP页面上显示URL的HTTP状态代码(例如404、200等)? - How to display a HTTP status code of a url (For example, 404, 200, etc. ) on Spring's JSP page? 如何连接两个用户,以便他们可以使用J2EE Web技术(JSP,Spring等)共享公共对象(游戏实例)? - How can I connect two users so they can share common object (game instance) with use of J2EE web technologies (JSP, Spring etc.)? 在jsp应用程序中导航时如何保持下拉菜单的值? - how to keep the value of dropdowns while navigating in jsp application? 如何使用JSP文件做模板 - How to template with JSP files 如何将应用程序(.exe 等)嵌入 jsp 页面 - How to embed an application (.exe etc) into a jsp page 从数据库填充的JSP下拉列表 - JSP dropdowns populated from database 如何在jsp中使用液体模板? - How to use liquid template in jsp? 如何在jsp中写相对URL? - How to write relative url in jsp? 如何使用 Action 类中的 Map 对象在 JSP 中创建动态下拉列表? - How can I make a dynamic dropdowns in a JSP using a Map object in an Action class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM