簡體   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