Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Spring MVC中将子类属性映射到表单路径_Java_Spring_Spring Mvc - Fatal编程技术网

Java 在Spring MVC中将子类属性映射到表单路径

Java 在Spring MVC中将子类属性映射到表单路径,java,spring,spring-mvc,Java,Spring,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 = "shortDescripti

产品

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

控制器

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

产品-Acme网络商店
函数deleteProduct(productId){
如果(确认('是否要删除此产品?')){
var url='delete/'+productId;
window.location.href=url;
}
}
产品商店-Acme Web商店

要添加新产品,请单击

图书馆产品目录 产品名称 产品简介 产品ISBN 产品格式 行动 ${product.title} ${product.shortDescription} ${product.isbn} ${product.format} " title=“删除产品” onclick=“javascript:deleteproduct(${product.id})”/>
我得到这个错误:
bean类[com.mycompany.app.model.Product]的无效属性'isbn':bean属性'isbn'不可读或具有无效的getter方法:getter的返回类型是否与setter的参数类型匹配?

我知道我做得不对,必须有一种方法将产品转换为书籍,这样我才能获得ISBN属性,我们如何做到这一点?

只需更改即可

@ModelAttribute("product") Product product

当Spring看到类型
Product
时,它将创建一个
Product
对象,而不是
Book
对象。显然,
Product
没有名为
isbn
的属性,因此您不能期望它为该属性解析


您可能想澄清您正在尝试做什么。当您需要子类型时,您不能使用超类型。

让我们看看您的
绑定和您拥有的getter/setter。我在这里发布的jjsp代码片段位于“是”中,我希望看到它。我还希望看到您在哪里填充模型。最后发布了整个JSP,让我们看看您将
product
添加到模型属性的位置。问题是我可以拥有所有类型的产品(即继承产品的东西),我不希望每个页面都有不同的页面…我希望能够添加一个产品,无论它是一本书、一张DVD,还是一个游戏,并以其独特的方式显示每个产品attribute@ThaSaleni我想不出一种方法可以很好地利用继承。我认为你最好有不同的页面。@ThaSaleni,如果你不想eac有不同的页面h您应该为每种可用的产品类型手动进行对象组装。您能举例说明您所说的吗?@bart这并不难。只需检测产品类型并创建一个映射器类,将请求中的所有相关属性映射到指定的产品。
@ModelAttribute("product") Product product
@ModelAttribute("product") Book product