使用SpringMVC形式:选择以将逗号分隔的值绑定到多个下拉列表 [英] Using SpringMVC form:select to bind comma separated values to multiple dropdown lists

查看:205
本文介绍了使用SpringMVC形式:选择以将逗号分隔的值绑定到多个下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个jsp页面,用户可以在其中从下拉列表中选择值,例如:

I am designing a jsp page, where users can select values from a dropdown like:

第1行:选择下拉列表"

Row1: Select "dropdown"

第2行:选择下拉菜单"

Row2: Select "dropdown"

第3行:选择下拉列表1",下拉列表2"

Row3: Select "dropdown1" "dropdown2"

其中:

下拉列表的值为{1,2,3,4,5,6,7}

dropdown values are {1,2,3,4,5,6,7}

用户可以从上面的行中选择值,然后保存表格.下次用户查看页面并从数据库中检索保存的值时,我想在下拉列表中显示它们.为了 我正在使用Spring MVC形式:选择自动绑定

Users can select the values from the above rows and save the form. The next time the user views the page and the saved values are retrieved from the database, I want to display them in the dropdowns. For that I am using Spring MVC form:select automatic binding

<form:select id="${id}" path="Mappings[${index}].userSetting">
            <c:forEach var="item" items="${dropdownValues}">
                <form:option value="${item.value}"><spring:eval expression="item" /></form:option>
            </c:forEach>
</form:select>

该代码对于仅绑定1个下拉列表的Row1和Row2正常工作.但是对于第3行, Mappings [$ {index}].userSetting返回"2,3"(而不是单个值)之类的值,这些值来自dropdownValues列表,但以逗号分隔.在这种情况下,出于明显的原因 Spring MVC form:select无法从下拉列表中选择值,因为在任一下拉列表中均找不到"2,3". 我正在尝试做的是拆分值,以便从"2,3"中选择值"2"和"3",以使Row3看起来像这样:

The code works fine for Row1 and Row2 which only have 1 dropdown to bind to. But in case of Row3, the Mappings[${index}].userSetting returns values like "2,3" (instead of a single value), which are from the dropdownValues list but comma delimited. In this case, for obvious reasons Spring MVC form:select fails to select values from the dropdown because "2,3" cannot be found in either of dropdowns. What I am trying to do is to split the values so that from the "2,3", the value "2" and "3" are selected such that Row3 looks like:

之前:选择下拉列表1"下拉列表2" 之后:选择"2""3"

Before: Select "dropdown1" "dropdown2" After: Select "2" "3"

有人对如何做到这一点有建议吗?

Does anyone have a suggestion for how to accomplish this?

推荐答案

您是否考虑过以下是您可以做什么的示例:

Here an example of what you could do :

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

<c:set var="row3Value" value="${fn:split(model.getMappings()[${index}].userSetting, ',')}" />

<form:select id="${id}" path="Mappings[${index}].userSetting">
    <c:forEach var="item" items="${dropdownValues}">
        <c:choose>
            <c:when test="${row3Value[0] eq ${item.value}}">
                <form:option selected="true" value="${item.value}"><spring:eval expression="item" /></form:option>
            </c:when>

            <c:otherwise>
                <form:option value="${item.value}"><spring:eval expression="item" /></form:option>
            </c:otherwise
        </c:choose> 
    </c:forEach>
</form:select>

这篇关于使用SpringMVC形式:选择以将逗号分隔的值绑定到多个下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆