2014-01-20 79 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