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;
}
}
}


Reply With Quote
