我在jsp中有一个包含类别的列表,当我单击某个类别时,列表中会填充类别的产品。当我单击该列表中的某个产品时,需要将该产品添加到jsp中的另一个列表中。我遇到的问题是,当我单击某个类别时,来自某个类别的产品被添加到第二个列表中,因为当我发布一个类别时,productdto的id也会更改.
以下是表格:
<c:forEach items="${categories}" var="item" >
<c:choose>
<c:when test="${item.getClass().name=='domain.Categorie'}">
<form:form method="POST" action="shoppinglist.htm" modelAttribute="categoriedto">
<form:input path="id" type="hidden" value="${item.id}" />
<input type="submit" height="150px" width="180px" alt="Submit" value="${item.categorieName}" class="productlistbtn">
</form:form>
</c:when>
<c:otherwise>
<form:form method="POST" action="shoppinglist.htm" modelAttribute="productdto">
<form:input path="id" type="hidden" value="${item.id}" />
<input type="submit" height="150px" width="180px" alt="Submit" value="${item.description}" class="productlistbtn">
</form:form>
</c:otherwise>
</c:choose>
</c:forEach> 这是控制器中的post方法:
@RequestMapping(value = {"/shoppinglist"},method = RequestMethod.POST)
public String showShoppinglistPage
(@ModelAttribute("categoriedto") CategorieDto categoriedto,
@ModelAttribute("productdto") ProductDto productdto,Model model)
{
if(productdto.getId()!=0)
{
List<Product> producten=new ArrayList<Product>();
Product p = categoryRepository.findProductBy(productdto.getId());
producten.add(p);
model.addAttribute("products", producten);
}
model.addAttribute("categories", categoryRepository.findBy(categoriedto.getId()).getProducts());
return "shoppinglist";
}发布于 2013-12-15 03:44:46
我知道你们有两份表格。所以,它会发布所有的值。我认为您需要再次编辑表单来解决问题。
https://stackoverflow.com/questions/20584473
复制相似问题