如何解决在对象内提交Hashmap的JSP表单绑定
我无法弄清楚如何将地图数据发送/绑定到控制器。这是详细信息 我下面有产品和价格数据类(未提供获取器和设置器),为此,我正在为其构建JSP表单以将产品数据提交给控制器。我可以从产品中读取ID,名称和说明,但是对于地图来说,价格却为空。我要编辑并提交价格地图的两个关键值。
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotNull
@Size(max = 50)
@Column(nullable = false)
private String name;
@Column(columnDefinition = "CLOB")
private String description;
@OneToMany(mappedBy = "product")
@MapKey(name = "currency")
private Map<Currency,Price> prices = new HashMap<>();
}
@Entity
public class Price {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false)
private Currency currency;
@Column(nullable = false)
private BigDecimal amount;
@ManyToOne
@JoinColumn(nullable = false)
private Product product;
下面是我要尝试的JSP
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html>
<head>
</head>
<body>
<h3>Here you can edit Product Details</h3>
<form:form method="POST"
action="/edit/product" modelAttribute="Product">
<table>
<tr>
<td><form:label path="id">Id</form:label></td>
<td><form:input path="id" value="${product.id}" readonly="true"/></td>
</tr>
<tr>
<td><form:label path="name">Name</form:label></td>
<td><form:input path="name" value="${product.name}" /></td>
</tr>
<tr>
<td><form:label path="description">Description</form:label></td>
<td><form:input path="description" value="${product.description}" /></td>
</tr>
<tr>
<c:forEach items="${Product.prices}" var="priceEntry">
<tr>
<td>Currency</td>
<td><input path="prices[${priceEntry.key}]" value="${priceEntry.key}" /></td>
<td>Price</td>
<td><input path="prices[${priceEntry.value.amount}]" value="${priceEntry.value.amount}" /></td>
</tr>
</c:forEach>
</tr>
<td><input type="submit" value="Submit"/></td>
</tr>
</table>
</form:form>
</body>
<ul>
<li><a href="/product/list">View Product List</a></li>
</ul>
</html>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。