Announcement Announcement Module
Collapse
No announcement yet.
errors lost when model.addAttribute(bean) is used. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • errors lost when model.addAttribute(bean) is used.

    Hi with spring 3.0.7 and jsp, the validation error messages are lost when I push the "bean" into the model, like so:

    controller:
    Code:
    @RequestMapping(value="/editProduct", method=RequestMethod.POST)
        public synchronized String submitEditedProduct(
                         @ModelAttribute("productbean") @Valid final ProductBean productbean,
                         BindingResult result, ModelMap map,
                         HttpServletRequest req)
        {	
                ProductBean tmp = products.get(Integer.valueOf(productbean.getProductId()));
    
                String productId = tmp.getProductId();
    
                if (result.hasErrors()) {
                    logger.debug("=====has errors messages: "+result.toString());
                    req.getSession().setAttribute("productId", productId);
    
                  map.addAttribute("productbean",tmp);  // <<--- if I commented this line, error messages will shown on the page.
    
                    return "admin_product/editProduct";
                }
    editProduct.jsp page:

    Code:
    <form:form method="post" modelAttribute="productbean" action="" id="form1">
    ...
    <tr>
                        <td width="118"><form:label for="salePrice" path="salePrice" > Sale Price:</form:label></td>
                        <td colspan="2"><form:input path="salePrice" id="salePrice"/></td>
                    </tr>
                    <tr>
                        <td></td>
                        <td colspan="2"><form:errors path="salePrice" cssClass="errorBlock" /></td>
                    </tr>
    ...
    </form:form>
    In the controller code, if I remove "map.addAttribute("productbean",tmp);", the error messages will show up.
    Since my productbean contains other values that need to be display on the jsp page when error occurs, so I need to push the productbean over to the editProduct.jsp page with the errors. What is the best way to allow me to push the productbean to jsp page while allow errors display on the page?


    Any suggestion is very appreciated.
    Thanks
    Sam
Working...
X