Nav
Nav

Reputation: 447

Spring MVC: Required Date parameter 'date' is not present

I'm writing a web application using Spring MVC. In my jsp is a form which content is send with Ajax to my controller. The simplified code for the mapped method looks:

@RequestMapping(value = "/filter", method = RequestMethod.POST)
public String filter(@RequestParam(required=false) Date createdTo) { }

I have registered CustomDateEditor with:

@InitBinder
public void registerBinders(WebDataBinder binder){
    SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
    binder.registerCustomEditor(Date.class, new CustomDateEditor(format, true));
}

When the String in the form input is correct no exception is thrown. The problem is that, when the input for date is empty or contains String that can't be converted to date, I get exception: Required Date parameter 'createdTo' is not present.

I've even written my own DateEditor and during debugging it, I've noticed that the problem occurs when the editor sets returned value to null with setValue(null). When I changed the code so the returned value is the current date setValue(new Date()) everything works even for empty Strings.

Why my mapped method doesn't allow to have `@RequestParam(required=false) Date createdTo' null value? Why is it so and how can I fox it?

The request is send with:

$.ajax({
type: 'POST',
url: '<c:url value="/admin/documents/filter"/>',
data: data
}).done(doneFunction);

The controller code is:

@Controller

@RequestMapping("/admin/documents") public class AdminDocumentsController extends BaseController {

@InitBinder
public void registerBinders(WebDataBinder binder) {
    binder.registerCustomEditor(Date.class, new DateEditor());
}

@RequestMapping(value = { "", "/" }, method = RequestMethod.GET)
public String showDocuments(Model model) {
    model.addAttribute("so", new DocumentsSearchOptions());
    return "admin/documents";
}

@RequestMapping(value = "/filter", method = RequestMethod.POST)
public @ResponseBody Map<String, Object> filter(@RequestParam String number,
        @RequestParam String invoiceNumber,
        @RequestParam String userSurname, @RequestParam(required=false) Double fromPrice,
        @RequestParam(required=false) Double toPrice, @RequestParam(required=false) Integer fromPoints,
        @RequestParam(required=false) Integer toPoints, @RequestParam String createdFrom,
        @RequestParam(required=false) Date createdTo, @RequestParam String invoiceFrom,
        @RequestParam String invoiceTo, @RequestParam String paidFrom,
        @RequestParam String paidTo) {

    Map<String, Object> model = new HashMap<>();

    return model;
}

Dates and object wrappers throw MissingServletRequestParameterException when their corresponding input fields are left empty.

Upvotes: 1

Views: 12214

Answers (1)

a better oliver
a better oliver

Reputation: 26828

The exception you get (MissingServletRequestParameterException) is really only thrown when a parameter is required, but not present. The parameter can even be empty.

Moreover binding happens after the parameter check. That means when you get the exception it's before the custom editor is called.

The problem must be related to another method. Maybe you redirect in the filtermethod.

Upvotes: 1

Related Questions