Announcement Announcement Module
Collapse
No announcement yet.
Spring 3.0 - Form validation issue Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring 3.0 - Form validation issue

    All

    I'm new to Spring 3.0 and was trying the create a form validation sample applicication but could not successfully complete it.

    It is the same Spring MVC step by step and I'm using Spring 3.0 features to complete the same.

    Following is the error I get:
    java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'priceIncrease' available as request attribute

    It seems to be familiar but I tried going thro all the threads available in spring forums and I could not fix the issue.

    Attached are my code snippets:
    FormController.java
    priceincrease.jsp
    springapp-servlet.xml

    Unlike Spring 2.5.x, Spring 3.0 used extensively annotation and not sure how the @ModelAttribute works and how the formbackobject(command in default) is populated by Spring.

    Spent a lot of time trouleshooting it. Need your expertise in solving this.

    Controller code:/**
    *
    */
    package demo.spring.web;
    .
    .
    .
    @Controller
    @SessionAttributes
    public class FormController {

    protected final Log logger = LogFactory.getLog(getClass());

    private static final String FORM_VIEW = "priceincrease";
    private static final String SUCCESS_VIEW = "redirect:hello.do";

    @Autowired
    private ProductManager productManager;


    @RequestMapping(value = "/priceincrease", method = RequestMethod.GET)
    public String setupFormView(ModelMap modelMap) {
    System.out.println("ENTERED setupFormView METHOD IN FORM CONTROLLER");
    PriceIncrease priceIncrease = new PriceIncrease();
    modelMap.addAttribute("priceIncrease",priceIncreas e);
    return FORM_VIEW;
    }


    @RequestMapping(value = "/priceincrease", method = RequestMethod.POST)
    public String priceIncrease(
    @ModelAttribute("priceIncrease") PriceIncrease priceIncrease,
    BindingResult result) {
    System.out.println("ENTERED priceIncrease METHOD IN FORM CONTROLLER");
    new PriceIncreaseValidator().validate(priceIncrease, result); // The result object will store the validation error if any
    if(result.hasErrors()) {
    return FORM_VIEW;
    }
    else {
    int increaseValue = priceIncrease.getPercentage();
    System.out.println("Increasing prices by " + increaseValue + "%.");
    productManager.increasePrice(increaseValue);
    return SUCCESS_VIEW;
    }
    }


    }
    Last edited by arvind.devarajan; Dec 24th, 2010, 12:25 AM. Reason: Adding the code snippet

  • #2
    You could you please include your controller code in the post. I can't download your files.

    Comment


    • #3
      Resolved

      This has been resolved.

      Issue: Not setting the domainObject (PriceIncrease.java)in the Model object in FormController. setupFormView(). Hence I was getting the error. On adding these 2 lines was able to proceed.

      PriceIncrease priceIncrease = new PriceIncrease();
      modelMap.addAttribute("priceIncrease",priceIncreas e);

      Thanks for taking time to look into it.

      Thanks
      Arvind

      Comment


      • #4
        Code:
        Unlike Spring 2.5.x, Spring 3.0 used extensively annotation and not sure how the @ModelAttribute works and how the formbackobject(command in default) is populated by Spring.
        Just in case someone asks the same question, I've prepared a guide for the @ModelAttribute.

        See http://krams915.blogspot.com/2010/12...ribute-in.html

        Comment

        Working...
        X