Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

You need to declare a name of your model attribute that matches modelAttribute in <form>:

... @ModelAttribute("job") ExportJob job, ...

By default attribute name would be inferred from attribute's class name (exportJob), and it won't match modelAttribute in your case.

share|improve this answer
    
yeah, that's right! thanks –  davioooh Jul 8 '13 at 9:52
add comment

The following code should show you errors that occur during databinding.

@RequestMapping(value = "/save", method = RequestMethod.POST)
public String saveJob(@ModelAttribute ExportJob job, BindingResult result, 
    ModelMap model) {

    //Debug errors, assumes logger is in place
    if (result.hasErrors()) {
       List<String> fieldErrors = new ArrayList<String>();
       for (FieldError f : result.getFieldErrors()) {
           fieldErrors.add(f.getField() + "=" + f.getRejectedValue().toString());
           log.debug("BindingFailure: {} = {}", new Object[] { f.getField(),
                 f.getRejectedValue() });
    }

    ExportJobValidator ejValidator = new ExportJobValidator();
    ejValidator.validate(job, result);

    if (result.hasErrors()) {
        return VIEW_EDIT_FORM;
    }
    schedulingService.saveAndSchedule(job);
    return "redirect:/schedule";
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.