I'm having a strange problem handling form submission using Spring MVC.
I'm using Spring 3.1.1 and I'm implementing a simple form to insert/edit an entity into my DB. My code is the following:
JSP
<sf:form action="save" method="post" modelAttribute="job">
<sf:hidden path="id" />
<span class="wrap">Label:</span> <sf:input path="label" />
<sf:errors path="label"></sf:errors>
<span class="wrap">Description:</span> <sf:textarea path="description" />
<input type="submit" value="save" />
</sf:form>
CONTROLLER
@RequestMapping(value = "/edit")
public String editJob(Integer jobId, Model model) {
ExportJob job = new ExportJob();
if (jobId != null && jobId > 0) {
job = schedulingService.getScheduledJob(jobId);
}
model.addAttribute("job", job);
return VIEW_EDIT_FORM;
}
@RequestMapping(value = "/save", method = RequestMethod.POST)
public String saveJob(@ModelAttribute ExportJob job, BindingResult result) {
ExportJobValidator ejValidator = new ExportJobValidator();
ejValidator.validate(job, result);
if (result.hasErrors()) {
return VIEW_EDIT_FORM;
}
schedulingService.saveAndSchedule(job);
return "redirect:/schedule";
}
When model attribute validation fails I expect to be redirected to the form and the errors to be displayed. Instead I get this error:
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'job' available as request attribute
.
I tried passing the job
in the saveJob
method, but this way no error is shown...
I'm not an expert of Spring MVC, so can you help me understand what I'm doing wrong?