Spring MVC HashMap 表单集成
Spring MVC HashMap Form Integration
我在模型对象中有一个哈希映射。表单提交时如何绑定hash map
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=""/>
}
但是没用。请帮助我如何给出正确的输入名称以绑定哈希映射
只需使用简单的 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 属性。
我认为你有两个错误:
- 您的地图的键为字符串,但您的
${emp.id}
可能是整型或长整型,请尝试使用引号。
- 在地图中你有员工列表,这个列表不能有名字,你可以删除它。
这样试试:
<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}
我在模型对象中有一个哈希映射。表单提交时如何绑定hash map
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=""/>
}
但是没用。请帮助我如何给出正确的输入名称以绑定哈希映射
只需使用简单的 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 属性。
我认为你有两个错误:
- 您的地图的键为字符串,但您的
${emp.id}
可能是整型或长整型,请尝试使用引号。 - 在地图中你有员工列表,这个列表不能有名字,你可以删除它。
这样试试:
<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}