2014-01-20 51 views
3

产品映射一个子类属性在Spring MVC形成路径

@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?

我知道我不是这样做的权利,必须有铸造产品手册,以便我能得到ISBN财产的一种方式,我们怎么办呢?

+0

让我们看看你的''绑定和你有的getters/setters。 –

+0

我在这里发布的jjsp代码片段位于 ThaSaleni

+0

的内部,是的,我希望看到它。我也想看看你在哪里填充你的模型。 –

回答

2

只要改变

@ModelAttribute("product") Product product 

@ModelAttribute("product") Book product 

春看到类型Product,它会创建一个Product对象,而不是Book对象。显然,Product没有名为isbn的属性,所以您不能指望它为该属性解析<form:input>

你可能想澄清你正在尝试做什么。当你期待子类型时,你不能使用超类型。

+0

事情是我可以拥有所有类型的产品(即继承产品的东西),我不想为每个产品都有不同的页面...我希望能够添加产品这是一本书,或DVD,或游戏,并显示每个具有其独特的属性 – ThaSaleni

+0

@ThaSaleni我想不出一种方式,你可以做很好的继承。我认为你最好有不同的页面。 –

+0

@ThaSaleni如果你不想为每个页面设置不同的页面,你应该手动为每个可用的产品类型进行对象组装。 – Bart