Results 1 to 4 of 4

Thread: Spring 3.0 - Form validation issue

  1. #1

    Question 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 23rd, 2010 at 11:25 PM. Reason: Adding the code snippet

  2. #2

    Default

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

  3. #3

    Smile 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

  4. #4
    Join Date
    Dec 2010
    Posts
    315

    Default

    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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •