java - 如何在 Spring 中从 UI 获取对象列表?

标签 java spring-mvc

我已在 UI 上显示了数组列表值。 Arraylist 包含 pojo 类“Preference” Preference 类有 3 个 String 成员 PreferenceName、PreferenceType 和 PreferenceValue。
在 jsp 中,我迭代了 arraylist 并显示了 PreferenceName ,以及它的值(使用单选按钮显示),无论它是 true 还是 false。
现在当用户提交表单时。我想要包含值的数组列表。我正在使用 Spring。我正在获取其他值,但有什么方法可以获取数组列表值。那么我需要做什么..?

                        <c:forEach items="${preferences.preferenceList}" var="preference">
                             <tr height="35px" bgcolor="#fafafa" bordercolor="#FFF">
                                <td width="50%"><c:out value="${preference.preferenceName}"/></td>
                                <td width="20%">
                                    <c:if test="${preference.preferenceType=='boolean'}">
                                     <c:if test="${preference.preferenceValue=='true'}">
                                        <input type="radio" name="${preference.preferenceName}" value="true" checked="true">Yes &nbsp;
                                        <input type="radio" name="${preference.preferenceName}" value="false" >No &nbsp;
                                     </c:if>
                                      <c:if test="${preference.preferenceValue=='false'}">
                                        <input type="radio" name="${preference.preferenceName}" value="true" > Yes &nbsp;
                                        <input type="radio" name="${preference.preferenceName}" value="false" checked="true" >No 
                                       </c:if>
                                    </c:if>
                                </td>
                             </tr>
                         </c:forEach>                                                                                                 

最佳答案

试试这个:

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<c:forEach items="${preferences.preferenceList}" var="preference" varStatus="itemRow">
    <c:set var="varPath" value="preferenceList[${itemRow.index}]"/>
    <tr height="35px" bgcolor="#fafafa" bordercolor="#FFF">
        <td width="50%">
            <form:label path="${varPath}.preferenceName">${preference.preferenceName}</form:label>
        </td>
        <td width="20%">
            <c:if test="${preference.preferenceType=='boolean'}">
                <form:radiobutton path="${varPath}.preferenceValue" value="true">Yes</form:radiobutton>
                <form:radiobutton path="${varPath}.preferenceValue" value="false">No</form:radiobutton>
            </c:if>
        </td>
    </tr>
</c:forEach>

关于java - 如何在 Spring 中从 UI 获取对象列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18631159/

相关文章:

java - 标签或页面片段中使用的 Spring-MVC 通用模型属性

java - 切换到 4.0.1 版本后 "JavaMailSenderImpl cannot be resolved to a type"

java - :each in <select> HTML tag. Thymeleaf 和 Spring 出现问题

java - 如何使用 Spring Boot Gradle Plugin 2.1.* 构建不可执行的 jar(库)

没有参数的springboot WireMock mock

java - 使用rest来通信两个基于表单的springboot应用程序

java - 如何在 Spring MVC 3 中添加 Controller ?

java - 为什么在 IDEA 12.1 中尝试调试 Java Spring 项目时 Jetty 服务器挂起?

java - 创建一个唯一的对象列表 Java

java - JTable 没有从数据库附加数据