HTTP状态500 - 在第64行处理JSP页面/WEB-INF/views/Product.jsp时发生异常

时间:2016-07-29 21:41:05

标签: java spring hibernate jsp

我是spring form和hibernate的新手,我有3个域对象:Product,Category和Supplier。每当我尝试在产品的页面上选择编辑链接时,都会出现以下异常。类别名称&供应商应显示在选择列表中,并且应将id值传递给属性。虽然添加操作和删除操作工作正常但不是编辑链接,但实际上必须使用将由用户选择的特定产品的值回发相同的页面。请帮忙。

Product.jsp

            <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
                pageEncoding="ISO-8859-1"%>
            <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
            <%@ taglib prefix="springtags" uri="http://www.springframework.org/tags"%>
            <%@ taglib uri="http://www.springframework.org/tags/form"
                prefix="spring"%>
            <%@ taglib prefix="springtags" uri="http://www.springframework.org/tags"%>
            <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
            <html>
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">


            <title>Product</title>
            </head>
            <body>

                <%@ include file="header.jsp"%>
                <br>
                <br>
                <br>
                <div style="height: 300px; width: 500px; padding: 10px" align="center">
                    <spring:form method="POST"
                        action="${pageContext.request.contextPath}/addProduct" commandName="product">
                        <table cellpadding="5" cellspacing="10" style="background-color: pink;">
                            <tr>
                                <c:choose>
                                    <c:when test="${! empty product.productname}">
                                        <td><spring:label path="productid"><springtags:message text="Product ID :"></springtags:message></spring:label></td>
                                        <td><spring:input path="productid" disabled="true" readonly="true" /></td>
                                    </c:when>
                                    <c:otherwise>
                                        <td><spring:label path="productid"><springtags:message text="Product ID :"></springtags:message></spring:label></td>
                                        <td><spring:input path="productid" /></td>
                                    </c:otherwise>
                                </c:choose>
                            </tr>
                            <tr>
                                <td>Product Name :</td>
                                <td><spring:input path="productname" /></td>

                            </tr>
                            <tr>
                                <td>Product Description :</td>
                                <td><spring:input path="description" /></td>
                            </tr>
                            <tr>
                                <td>Product Price :</td>

                                <td><spring:input path="price" /></td>
                            </tr>
                            <tr>

                                <td>Category :</td>
                                <td><spring:select path="categoryid"
                                        items="${categorylist}" itemValue="id" itemLabel="name">

                                    </spring:select>
                                <td><spring:errors path="categoryid" cssClass="error" /></td>
                            </tr>
                            <tr>

                                <td>Supplier :</td>
                                <td><spring:select path="supplierid"
                                        items="${supplierlist}" itemValue="id" itemLabel="name" />
                                <td><spring:errors path="supplierid" cssClass="error" /></td>
                            </tr>
                            <tr>
                                <td align="center">
                                <c:if test="${product.productid eq 0}">
                                    <input type="submit" value="<springtags:message text="Add Product"/>" />
                                </c:if>
                                </td>
                                <td>
                                <c:if test="${product.productid ne 0}">
                                    <input type="submit" value="<springtags:message text="Edit Product"/>" />
                                </c:if>
                                </td>
                            </tr>


                        </table>
                    </spring:form>
                    <div>
                        <p>${msg}</p>
                    </div>
                    <div class="container">
                        <table
                            class="table table-striped table-bordered table-hover table-condensed">
                            <caption>
                                <h2>Categories</h2>
                            </caption>
                            <thead>
                                <tr>
                                    <th>Product ID</th>
                                    <th>Product Name</th>
                                    <th>Product Description</th>
                                    <th>Product Price</th>
                                    <th>Category Name</th>
                                    <th>Supplier Name</th>
                                    <th>Edit</th>
                                    <th>Delete</th>
                                </tr>
                            </thead>
                            <c:if test="${not empty productlist}">
                                <tbody>
                                    <c:forEach items="${productlist}" var="product">

                                        <tr>
                                            <td>${product.productid}</td>
                                            <td>${product.productname}</td>
                                            <td>${product.description}</td>
                                            <td>${product.price}</td>
                                            <td>${product.category.name}</td>
                                            <td>${product.supplier.name }</td>
                                            <td><a href="product/edit/${product.productid}">Edit</a></td>
                                            <td><a href="product/delete/${product.productid}">Delete</a></td>
                                        </tr>
                                    </c:forEach>
                                </tbody>
                            </c:if>
                            <c:if test="${empty productlist}">
                There are no product yet. 
            </c:if>
                            </tr>


                        </table>
                    </div>
            </body>

ProductController的

                package com.niit;

                import java.util.List;
                import com.niit.cakecuisinebe.model.Category;


                import javax.servlet.http.HttpServletRequest;
                import javax.servlet.http.HttpServletResponse;

                import org.apache.log4j.Logger;
                import org.springframework.beans.factory.annotation.Autowired;
                import org.springframework.stereotype.Controller;
                import org.springframework.ui.Model;
                import org.springframework.ui.ModelMap;
                import org.springframework.web.bind.annotation.ModelAttribute;
                import org.springframework.web.bind.annotation.PatchMapping;
                import org.springframework.web.bind.annotation.PathVariable;
                import org.springframework.web.bind.annotation.RequestMapping;
                import org.springframework.web.bind.annotation.RequestMethod;
                import com.niit.cakecuisinebe.model.Category;
                import com.niit.cakecuisinebe.dao.CategoryDAO;
                import com.niit.cakecuisinebe.dao.CategoryDAOImpl;
                import com.niit.cakecuisinebe.dao.ProductDAO;


                import com.niit.cakecuisinebe.dao.*;
                import com.niit.cakecuisinebe.model.*;
                @Controller
                public class ProductController{
                protected static Logger logger = Logger.getLogger("ProductController");

                    @Autowired
                    private ProductDAO productDao;
                    @Autowired
                    private CategoryDAO categoryDao;
                    @Autowired
                    private SupplierDAO supplierDao;

                    @RequestMapping(value = "/products", method = RequestMethod.GET)
                    public String getProduct(Model model) {

                        logger.info("entering showAllGreetings");


                        List<Category> categories=categoryDao.list();
                        List<Supplier> suppliers=supplierDao.list();
                        List<Product> products = productDao.list();

                        if (products!=null && !products.isEmpty()) {

                            model.addAttribute("product", new Product());
                            model.addAttribute("category", new Category());
                            model.addAttribute("supplier", new Supplier());
                            model.addAttribute("productlist", products);
                            model.addAttribute("categorylist",categories);
                            model.addAttribute("supplierlist",suppliers);
                        }

                        return "Product";
                    }

                    @RequestMapping(value = "/addProduct", method= RequestMethod.POST)
                    public String addProduct(@ModelAttribute("product") Product product ) {
                        logger.info("entering showAllGreetings");

                        /*System.out.println(product.getCategory().getId());
                        System.out.println(product.getSupplier().getId());

                        Category category = categoryDao.getByName(product.getCategory().getName());
                        categoryDao.saveOrUpdate(category); 
                        Supplier supplier = supplierDao.getByName(product.getSupplier().getName());
                        supplierDao.saveOrUpdate(supplier);

                        product.setCategory(category);
                        product.setSupplier(supplier);

                        product.setCategoryid(category.getId());
                        product.setSupplierid(supplier.getId());*/
                        productDao.saveOrUpdate(product);

                        return "redirect:/products";

                    }

                    @RequestMapping(value = "/product/delete/{id}", method = RequestMethod.GET)
                    public String deleteProduct(@PathVariable("id")int id, ModelMap model) {
                        logger.info("entering showAllGreetings");


                        productDao.delete(id);
                        model.addAttribute("msg","Successfully Deleted");
                        return "redirect:/products";
                    }

                    @RequestMapping(value = "/product/edit/{id}", method = RequestMethod.GET)
                    public String showEditProduct(@PathVariable("id") int id, ModelMap model ) {
                        logger.info("entering showAllGreetings");
                        Product productToUpdate=this.productDao.get(id);
                        model.addAttribute("product", this.productDao.get(id));
                        model.addAttribute("category", this.categoryDao.get(productToUpdate.getCategoryid()));
                        model.addAttribute("supplier", this.supplierDao.get(productToUpdate.getSupplierid()));
                        model.addAttribute("productlist", productDao.list());       
                        model.addAttribute("categorylist",this.categoryDao.list());
                        model.addAttribute("supplierlist",this.productDao.list());
                        return "Product";
                    }




                    }

Product.java

    package com.niit.cakecuisinebe.model;

    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToMany;
    import javax.persistence.ManyToOne;
    import javax.persistence.Table;

    import org.springframework.stereotype.Component;

    @Entity
    @Table
    @Component
    public class Product {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int productid;
    private String productname;
    private String description;
    private double price;
    private int categoryid;
    private int supplierid;

    public int getCategoryid() {
        return categoryid;
    }
    public void setCategoryid(int category_id) {
        this.categoryid = category_id;
    }
    public int  getSupplierid() {
        return supplierid;
    }
    public void setSupplierid(int supplier_id) {
        this.supplierid = supplier_id;
    }
    @ManyToOne
    @JoinColumn(name="categoryid",nullable = false, updatable = false, insertable = false)
    private Category category;

    @ManyToOne
    @JoinColumn(name="supplierid",nullable = false, updatable =false, insertable = false)
    private Supplier supplier;

    public Category getCategory() {
        return category;
    }
    public void setCategory(Category category) {
        this.category= category;
    }
    public Supplier getSupplier() {
        return supplier;
    }
    public void setSupplier(Supplier supplier) {
        this.supplier= supplier;
    }
    public int getProductid() {
        return productid;
    }
    public void setProductid(int productid) {
        this.productid = productid;
    }
    public String getProductname() {
        return productname;
    }
    public void setProductname(String productname) {
        this.productname = productname;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }

    }
    HTTP Status 500 - An exception occurred processing JSP page /WEB- INF/views/Product.jsp at line 64
    type Exception report

    message An exception occurred processing JSP page /WEB-INF/views/Product.jsp at line 64

    description The server encountered an internal error that prevented it from fulfilling this request.

    exception
    org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/views/Product.jsp at line 64

    61:                 <tr>
    62: 
    63:                     <td>Supplier :</td>
    64:                     <td><spring:select path="supplierid"
    65:                             items="${supplierlist}" itemValue="id" itemLabel="name" />
    66:                     <td><spring:errors path="supplierid" cssClass="error" /></td>
    67:                 </tr> 

......
root cause org.springframework.beans.NotReadablePropertyException: Invalid   property 'id' of bean class [com.niit.cakecuisinebe.model.Product] 

1 个答案:

答案 0 :(得分:0)

根据spring:select标记的documentation,属性itemValue应指向有效的bean属性。对于我在JSP上看到的内容,该代码块呈现Supplier信息......那么,您的Supplier类是否包含id实例属性(或bean属性)?如果没有,您应该将正确的属性放在itemValue标记属性...

但是,异常抱怨类Product没有id属性... ${supplierlist}表达式是否包含供应商对象或Product对象(您应该检查一下)...