1

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?

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.