产品
@Entity
@Table(name = "product")
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
public class Product {
@Id
@Column(name = "id")
@GeneratedValue()
private Integer id;
@Column(name = "shortDescription")
private String shortDescription;
书
@Entity
@DiscriminatorValue(value = "Book")
public class Book extends Product {
@Column(name = "isbn")
private String isbn;
控制器
@RequestMapping(value="/product/add",
method = RequestMethod.POST)
public String addProduct(@ModelAttribute("product")
Product product, BindingResult result
){
if(null == product.getId()){
productService.addProduct(product);
}else{
productService.updateProduct(product);
}
return "redirect:/";
}
jsp,我正在尝试显示此属性
<head>
<title>Products - Acme Web Store</title>
<script type="text/javascript">
function deleteProduct(productId){
if(confirm('Do you want to delete this product ?')){
var url = 'delete/'+productId;
window.location.href = url;
}
}
</script>
</head>
<body>
<h2>Product Store - Acme Web Store</h2>
<p style="color: green; font-weight: bold;">
To add a new product please click <a href="<c:url var="action" value="/product/add"></c:url>"> <img
src="<c:url value='/images/vcard_add.png' />"
title="Add a New Product" />
</a>
</p>
<c:url var="action" value="/product/add"></c:url>
<form:form method="post" action="${action}" commandName="product"
cssClass="productForm">
<table>
<c:if test="${!empty product.title}">
<tr>
<td><form:label path="id" cssClass="productLabel">
<spring:message code="label.productId" />
</form:label></td>
<td><form:input path="id" readonly="true" size="8"
disabled="true" /> <form:hidden path="id" /></td>
</tr>
</c:if>
<tr>
<td><form:label path="title" cssClass="productLabel">
<spring:message code="label.productTitle" />
</form:label></td>
<td><form:input path="title" /></td>
</tr>
<tr>
<td><form:label path="shortDescription" cssClass="productLabel">
<spring:message code="label.shortDescription" />
</form:label></td>
<td><form:input path="shortDescription" /></td>
</tr>
<tr>
<td><form:label path="isbn" cssClass="productLabel">
<spring:message code="label.isbn" />
</form:label></td>
<td><form:input path="isbn" /></td>
</tr>
<tr>
<td><form:label path="format" cssClass="productLabel">
<spring:message code="label.format" />
</form:label></td>
<td><form:input path="format" /></td>
</tr>
<tr>
<td colspan="2"><c:if test="${!empty product.productName}">
<input type="submit"
value="<spring:message code="label.editproduct"/>" />
</c:if> <c:if test="${empty product.productName}">
<input type="submit"
value="<spring:message code="label.addproduct"/>" />
</c:if></td>
</tr>
<tr>
<td><form:label path="type" cssClass="productLabel">
<spring:message code="label.type" />
</form:label></td>
<td>
<form:select path="type">
<form:option value="0" label="Select One" />
<form:option value="1" label="Book" />
<form:option value="2" label="Game" />
</form:select>
</td>
</tr>
</table>
</form:form>
<h3>List of products in Library</h3>
<c:if test="${!empty productList}">
<table class="productTable">
<tr>
<th width="160">Product Title</th>
<th width="190">Product Short Description</th>
<th width="80">Product ISBN</th>
<th width="80">Product Format</th>
<th width="60">Action</th>
</tr>
<c:forEach items="${productList}" var="product">
<tr>
<td><a href="<c:url value='/edit/${product.id}' />">${product.productName}</a>
</td>
<td>${product.title}</td>
<td>${product.shortDescription}</td>
<td>${product.isbn}</td>
<td>${product.format}</td>
<td><img src="<c:url value='/images/vcard_delete.png' />"
title="Delete product"
onclick="javascript:deleteproduct(${product.id})" /> <a
href="<c:url value='/edit/${product.id}' />"> <img
src="<c:url value='/images/vcard_add.png' />"
title="Edit product" />
</a></td>
</tr>
</c:forEach>
</table>
</c:if>
</body>
</html>
我收到此错误:
invalid property 'isbn' of bean class [com.mycompany.app.model.Product]: Bean property 'isbn' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
我知道我没有做到这一点,必须有一种方法将Product转换为Book以便我可以获得ISBN属性,我们该怎么做?
答案 0 :(得分:2)
只需更改
@ModelAttribute("product") Product product
到
@ModelAttribute("product") Book product
当Spring看到类型Product
时,它将创建一个Product
对象,而不是Book
对象。显然,Product
没有名为isbn
的属性,因此您不能指望它为该属性解析<form:input>
。
您可能想澄清您要做的事情。当您期望子类型时,不能使用超类型。