java.lang.IllegalStateException:BindingResult和bean名称&product;'产品'可用作请求属性

时间:2016-09-22 11:02:28

标签: java spring jsp spring-mvc spring-form

每当我点击编辑按钮时,我都会收到此错误

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'product' available as request attribute

,无法理解这里的错误

我的产品控制器如下

@RequestMapping(value="/product/edit/{id}", method=RequestMethod.GET)
public String EditProducts(@PathVariable("id") String ProductID,Model model){
    System.out.println("ProductID");
    product = productDAO.get(ProductID);
    model.addAttribute("product", product);
    model.addAttribute("ProductList",productDAO.list());
    return "product";
}

我的product.jsp页面如下

<c:url var="product" value="/product/add" />
<form:form method="POST" commandName="product" action="${product}" enctype="multipart/form-data">
<div class="form-group">
<table class="table table-hover">
<tr>
   <td><form:label path="id"><spring:message text="ID" /></form:label></td>
   <c:choose>
   <c:when test="${!empty listValue.getId()}">
   <td><form:input path="id" disabled="true" readonly="true"/></td>
   </c:when>
   <c:otherwise>
   <td><form:input class="form-control" path="id" pattern=".{4,7}" required="true" title="id should contain 4 to 7 characters" /></td>
   </c:otherwise>
   </c:choose>
</tr>
<tr>
    <td><form:label path="name">Name</form:label></td>
    <td><form:input class="form-control" path="name" type="text" required="true"/></td>
</tr>
<tr>
    <td><form:label path="price">Price</form:label></td>
    <td><form:input class="form-control" path="price" type="text" required="true"/></td>
</tr>
    <tr>
    <td><form:label path="category_id">Category ID</form:label></td>
    <td><form:input class="form-control" path="category_id" type="text" required="true"/></td>
</tr>
    <tr>
    <td><form:label path="supplier_id">Supplier ID</form:label></td>
    <td><form:input class="form-control" path="supplier_id" type="text" required="true"/></td>
</tr>
 <tr>
    <td><form:label path="imge">Image</form:label></td>
    <td><form:input path="imge" type="file" name="imge" /></td>
</tr>
<tr>     
<td colspan="2">
<c:if test="${!empty productList.name}">
<input class="btn btn-success" type="submit" value="<spring:message text="Edit Product"/>"/>
</c:if>
<c:if test="${empty productList.name}">
<input class="btn btn-info" type="submit" value="<spring:message text="Add Product"/>" />
</c:if></td>
</tr>
</table>
<br>
</div>
</form:form>
<center><h1>${msg}</h1></center>
<c:if test="${not empty ProductList}">
<center><h1>List</h1></center>
<table class="table table-hover">
    <thead>
      <tr>
        <th>Image</th>
        <th>product Id</th>
        <th>product Name</th>
        <th>product Price</th>
        <th>Category Name</th>
        <th>Supplier Name</th>
        <th>Action</th>
      </tr>
    </thead>
    <c:forEach var="listValue" items="${ProductList}" varStatus="status">
    <tbody>
               <tr>
                <td style="width:10% ;height:2%"><img style="width:2% ;height:2%" src='<c:url value="/resources\img/${listValue.getId()}.jpg"/>'/></td>                 
                <td>${listValue.getId()}</td>
                <td>${listValue.getName()}</td>
                <td>${listValue.getPrice()}</td>
                <td> 
                <c:out  value="${CategoryList[status.index]}"/> 
                </td>
                <td><c:out  value="${SupplierList[status.index]}"/></td>
                <td><a href="<c:url value='product/edit/${listValue.getId()}' />" class="btn btn-info">Edit</a> / <a href="<c:url value='product/remove/${listValue.getId()}' /> " class="btn btn-danger">Delete</a></td>
               </tr>
              </c:forEach>      
  </tbody>
  </table>
    </c:if>

单击编辑按钮时,我必须将填充的数据填入表单。我是新手,在本节遇到麻烦,我看过类似的问题,但无法理解,请帮忙。如果需要更多细节,我也会提供它们。

1 个答案:

答案 0 :(得分:0)

您必须提供产品对象,以便您的jsp可以加载其数据。例如,当你想在重新加载页面而不丢失数据后,它也可以帮助你。

@RequestMapping("/newproduct")
public ModelAndView form(@Valid Product product){
    ModelAndView mv = new ModelAndView("product");
    return mv;
}