简体   繁体   中英

Spring MVC form validation: missing model attribute object

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM