Spring form validate:Whitelabel Error Page

时间:2016-05-17 09:23:17

标签: spring

    @RequestMapping(method = RequestMethod.GET, value = "/add")
    public ModelAndView add() throws ConferenceNotFoundException {
        LOGGER.debug("Getting adding page");
        return new ModelAndView("conference/add", "form", new ConferenceForm());
    }

    @RequestMapping(method = RequestMethod.POST, value = "/add")
    public String handleAddConferenceForm(@Valid @ModelAttribute("form") ConferenceForm form,
                                          BindingResult bindingResult,
                                          @ModelAttribute("currentUser") CurrentUser currentUser) {
        LOGGER.debug("Processing add conference form={}, bindingResult={}", form, bindingResult);

        form.setHost(currentUser.getUser());

        if (bindingResult.hasErrors()) {
            // failed validation
            return "conference/add";
        }
        try {
            conferenceService.create(form);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // ok, redirect
        return "redirect:/";
    }

enter image description here

我在代码上方制作弹簧形式。它的效果就像图片上方一样。

@RequestMapping(method = RequestMethod.GET, value = "/{id}/admin/update")
public ModelAndView update(Model model,
                           @PathVariable("id") Long id) throws ConferenceNotFoundException {
    LOGGER.debug("Getting update page");

    Conference conference = conferenceService.findById(id);
    model.addAttribute("conference", conference);
    return new ModelAndView("conference/update", "form", new ConferenceForm(conference));
}

@RequestMapping(method = RequestMethod.POST, value = "/{id}/admin/update")
public String handleUpdateConferenceForm(@Valid @ModelAttribute("form") ConferenceForm form,
                                         @PathVariable("id") Long id,
                                         BindingResult bindingResult,
                                         @ModelAttribute("currentUser") CurrentUser currentUser) {
    LOGGER.debug("Processing update conference form={}, bindingResult={}", form, bindingResult);

    form.setHost(currentUser.getUser());

    if (bindingResult.hasErrors()) {
        // failed validation
        return "conference/update";
    }
    try {
        conferenceService.update(form, id);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // ok, redirect
    return "redirect:/conferences/" + id + "/admin";
}

enter image description here

否则,上面的代码不能正常工作。它的验证器运行良好,它更新内容。但是当验证器工作时它会生成Whitelabel Error Page

我不知道为什么会产生Whitelabel Error Page

1 个答案:

答案 0 :(得分:3)

我发现了问题所在。

更改BindingResult bindingResult的位置。

自:

public String handleUpdateConferenceForm(@Valid @ModelAttribute("form") ConferenceForm form,
                                     @PathVariable("id") Long id,
                                     BindingResult bindingResult,
                                       @ModelAttribute("currentUser") CurrentUser currentUser)

要:

public String handleUpdateConferenceForm(@Valid @ModelAttribute("form") ConferenceForm form,
                                             BindingResult bindingResult,
                                             Model model,
                                             @PathVariable("id") Long id,
                                             @ModelAttribute("currentUser") CurrentUser currentUser)