Java Spring 表单 ModelAttribute 字段验证以避免 400 Bad Request 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18323863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring form ModelAttribute field validation to avoid 400 Bad Request Error
提问by Mateusz
I've got an ArticleFormModelcontaining data sent by normal html formwhich is injected by Spring using @ModelAttributeannotation, i.e.
我有一个ArticleFormModel由普通发送的包含数据,html form它是由 Spring 使用@ModelAttribute注释注入的,即
@RequestMapping(value="edit", method=RequestMethod.POST)
public ModelAndView acceptEdit(@ModelAttribute ArticleFormModel model,
HttpServletRequest request, BindingResult errors)
{
//irrelevant stuff
}
Everything works perfectly fine up to some point. The problem is that the ArticleFormModelcontains a doublefield (protected, set using normal setter). Everything works fine as long as data sent by user is a number. When they type a word, all I get is 400 Bad Request Http Error.
在某些时候一切正常。问题是ArticleFormModel包含一个double字段 ( protected, 使用普通 setter 设置)。只要用户发送的数据是数字,一切都正常。当他们输入一个词时,我得到的只是400 Bad Request Http Error。
I've already registered a WebDataBinderfor this controller
我已经WebDataBinder为这个控制器注册了一个
@InitBinder
protected void initBinder(WebDataBinder binder) throws ServletException
{
binder.setValidator(validator);
}
where validatoris an instance of a custom class implementing org.springframework.validation.Validatorinterface
but I don't know what to do next. I'd like to be able to parse the model, get valid HTTP response and display error message in the form. The initBinder()method is called and I can call validator.validate()from it but it doesn't change the error (for that wrong data).
哪里validator是实现org.springframework.validation.Validator接口的自定义类的实例,但我不知道下一步该怎么做。我希望能够解析模型,获得有效的 HTTP 响应并在表单中显示错误消息。该initBinder()方法被调用,我可以从中调用validator.validate(),但它不会改变错误(对于错误的数据)。
I'm aware that I could use a setter to parse the string, check if it's a number, if not, store that info in a variable, then retrieve that variable during validation, but that seems to be too much work. There has to be an easier way to force a type on the field without getting an error. Also, the issue is in data binding, not validation, so I feel that it should be placed in the respective code layer.
我知道我可以使用 setter 来解析字符串,检查它是否是一个数字,如果不是,则将该信息存储在一个变量中,然后在验证期间检索该变量,但这似乎工作量太大。必须有一种更简单的方法来强制字段上的类型而不会出错。另外,问题在于数据绑定,而不是验证,所以我觉得应该放在各自的代码层。
I was also thinking about implementing java.beans.PropertyEditorand calling binder.registerCustomEditor(), but I'm lacking a reliable knowledge source.
我也在考虑实施java.beans.PropertyEditor和调用binder.registerCustomEditor(),但我缺乏可靠的知识来源。
Client-side validation (checking if data is number via JavaScript) isn't a possibility.
客户端验证(通过 JavaScript 检查数据是否为数字)是不可能的。
TL;DR:
特尔;博士:
How can I force a field to be of specific type for a @ModelAttributeitem without getting 400 Bad Request Http Error?
如何在@ModelAttribute不获取 的情况下强制字段为项目的特定类型400 Bad Request Http Error?
采纳答案by Shinichi Kai
You can use <form:errors>for a binding error.
您可以<form:errors>用于绑定错误。
It looks like this:
它看起来像这样:
Controller:
控制器:
@RequestMapping(value="edit", method=RequestMethod.POST)
public ModelAndView acceptEdit(@ModelAttribute ArticleFormModel model,
BindingResult errors, HttpServletRequest request)
{
if (errors.hasErrors()) {
// error handling code goes here.
}
...
}
errorsparameter is needed to be placed on the right after the model.
errors参数需要放在模型的右边。
See below for details (Example 17.1):
详情见下文(例 17.1):
jsp:
jsp:
<form:form modelAttribute="articleFormModel" ... >
...
<form:errors path="price" />
</form:form>
message properties file:
消息属性文件:
typeMismatch.articleFormModel.price=customized error message

