繁体   English   中英

Spring MVC HashMap表单集成

[英]Spring MVC HashMap Form Integration

我在模型对象中有一个哈希映射。 提交表单时如何绑定哈希图。

Map<String, List<Employee>> employeeMap= new HashMap< String, List<Employee>>(); 
List<Employee> employeeList;
//getters and setters of employeeMap and employeeList

员工对象是

class Employee{

String name;
String pincode;
String organization;

//getters and setter

}

表单输入值为

List{ //list iteration here

<input type="text" name="employeeMap[${emp.id}].employeeList[list_index].name" value=""/>
}

但这没用。 请帮助我如何为哈希映射提供正确的输入名称

我认为您有两个错误:

  1. 您的地图的键值为String,但是${emp.id}可能为int或long,请尝试使用引号。
  2. 在地图中,您有雇员列表,此列表不能有名称,您可以删除它。

尝试这样:

<input type="text" name="employeeMap["${emp.id}"][list_index].name" value=""/>

这是我类似的工作示例:

    User u1 = new User();
    u1.setEmailAddress("email1");
    User u2 = new User();
    u2.setEmailAddress("email2");

    u1List.add(u1);
    u1List.add(u2);
    u2List.add(u2);
    u2List.add(u1);

    userMap.put("1", u1List);
    userMap.put("2", u2List);

    model.addAttribute("userMap", userMap);

JSP:

Email of second user from map with key=1 = ${employeeMap["1"][1].emailAddress}

只需使用简单的JSTL foreach标记即可。

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

<c:forEach items="${employeeMap}" var="entry">
    <input type="text" name="${entry.key}" value="${entry.value}" />
</c:forEach>

通过${entry.value}您可以访问bean属性。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM