2016-09-22 121 views
0

每当我点击编辑按钮,我收到此错误java.lang.IllegalStateException:无论BindingResult也不是为bean名称“产品”可作为请求属性平原目标对象

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> 

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

回答

0

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

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