本文介绍了Spring形式ModelAttribute字段验证,以避免400 Bad Request Error的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ArticleFormModel 包含普通 html表单发送的数据,由Spring使用<$注入c $ c> @ModelAttribute 注释,即

I've got an ArticleFormModel containing data sent by normal html form which is injected by Spring using @ModelAttribute annotation, i.e.

@RequestMapping(value="edit", method=RequestMethod.POST)
public ModelAndView acceptEdit(@ModelAttribute ArticleFormModel model, 
    HttpServletRequest request, BindingResult errors)
{
    //irrelevant stuff
}

在某些方面,一切都很完美。问题是 ArticleFormModel 包含 double 字段( protected ,使用普通的setter设置)。只要用户发送的数据是数字,一切正常。当他们输入一个单词时,我得到的只是 400 Bad Request Http Error

Everything works perfectly fine up to some point. The problem is that the ArticleFormModel contains a double field (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.

我已经注册了此控制器的 WebDataBinder

I've already registered a WebDataBinder for this controller

@InitBinder
protected void initBinder(WebDataBinder binder) throws ServletException
{
    binder.setValidator(validator);
}

其中验证器是实现 org.springframework.validation.Validator 接口
的自定义类的实例,但我不知道接下来该做什么。我希望能够解析模型,获得有效的HTTP响应并在表单中显示错误消息。调用 initBinder()方法,我可以从中调用 validator.validate()但它不会改变错误(对于那个错误的数据)。

where validator is an instance of a custom class implementing org.springframework.validation.Validator interfacebut 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).

我知道我可以使用setter来解析字符串,检查它是否是一个数字,如果没有,则存储变量中的info,然后在验证期间检索该变量,但这似乎太多了。必须有一种更简单的方法来强制字段上的类型而不会出现错误。此外,问题是数据绑定,而不是验证,所以我觉得它应该放在相应的代码层。

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.

我还在考虑实现 java.beans.PropertyEditor 并调用 binder.registerCustomEditor (),但我缺乏可靠的知识源。

I was also thinking about implementing java.beans.PropertyEditor and calling binder.registerCustomEditor(), but I'm lacking a reliable knowledge source.

客户端验证(通过JavaScript检查数据是否为数字)不是'可能性。

Client-side validation (checking if data is number via JavaScript) isn't a possibility.

TL; DR:

我如何强迫字段是 @ModelAttribute 项的特定类型而没有获得 400错误请求Http错误

How can I force a field to be of specific type for a @ModelAttribute item without getting 400 Bad Request Http Error?

推荐答案

您可以使用< form:errors> 来解决绑定错误。

You can use <form:errors> for a binding error.

它看起来像这样:

控制器:

@RequestMapping(value="edit", method=RequestMethod.POST)
public ModelAndView acceptEdit(@ModelAttribute ArticleFormModel model, 
    BindingResult errors, HttpServletRequest request)
{
  if (errors.hasErrors()) {
    // error handling code goes here.
  }
  ...
}

错误参数需要放在模型后面的右边。

errors parameter is needed to be placed on the right after the model.

详见下文(例17.1):

See below for details (Example 17.1):

jsp:

<form:form modelAttribute="articleFormModel" ... >
  ...
  <form:errors path="price" />
</form:form>

消息属性文件:

typeMismatch.articleFormModel.price=customized error message

这篇关于Spring形式ModelAttribute字段验证,以避免400 Bad Request Error的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 15:45