1

我正在使用 Spring MVC 并且正在制作一个验证器,但看起来 Spring 从未运行它。

这是我的验证器现在很简单,只需检查两个字段

public class MemberRequestValidator  implements Validator {

    public boolean supports(Class aClass) {
        return MemberRequest.class.equals(aClass);
    }

    public void validate(Object obj, Errors errors) {

        MemberRequest mr = (MemberRequest) obj;

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "content", "Content field is Required");

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "areacode", "Area code field is Required");


    }

}

现在我的控制器如下所示:

 @InitBinder("memberrequest")
    public void initMemberRequestBinder(WebDataBinder binder) {
        binder.setValidator(new MemberRequestValidator());
    }



    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ModelAndView saveRequest(@ModelAttribute @Valid MemberRequest mr,  BindingResult result)
    {

        if (result.hasErrors())
        {
            LOGGER.debug("Pages had errors on it... returning to input page");
                return new ModelAndView("question");
        }
        else
        {
                String Ticket = mService.sentWebRequest(mr);

                Map<String, Object> model = new HashMap<String, Object>();
                Ticket t = new Ticket();
                t.setTicketDetails(Ticket);

                model.put("ticket", t);

                return new ModelAndView("thanks", model);
        }


    }

在我的 JSP 页面中,我有以下内容:

 <c:url var="saveUrl" value="/mrequest/save.html" />

            <form:form modelAttribute="memberrequest"  action="${saveUrl}" name="memberrequest" id="memberrequest">

所以如果我没有在表格中输入任何数据,我应该点击错误但我没有?

4

1 回答 1

2

尝试使用@ModelAttribute("memberrequest")in handler 或 modelAttribute="memberRequest" 在表单和@initBinder("memberRequest")

于 2013-03-07T20:48:47.733 回答