java Spring:如何解决验证错误 -> 错误代码 -> 错误消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4202437/
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: How to resolve a validation error -> error code -> error message
提问by David Parks
In Spring, after validation we get a BindingResult
object in the controller
.
在 Spring 中,经过验证后,我们BindingResult
在controller
.
Simple enough, if I get validation errors I want to re-display my form with the error message above each afflicted field.
很简单,如果我收到验证错误,我想重新显示我的表单,并在每个受影响的字段上方显示错误消息。
So to check for field errors on field username
of my FormObject
I call:
因此,要检查领域错误的现场username
我的FormObject
我的呼叫:
FieldError usernameFieldError = bindingResult.getFieldError("username");
Great, now I hold a FieldError
object which, assuming I'm using the DefaultMessageCodeResolver
now contains something like 4 possible error codes.
太好了,现在我持有一个FieldError
对象,假设我使用的是DefaultMessageCodeResolver
现在包含 4 个可能的错误代码的东西。
How do I go from FieldError
-> A string that is consumable to the user?
如何从FieldError
-> 用户可消费的字符串开始?
I have a MessageSource
defined in my webapplication context, so I can map a single error code to a message.
我MessageSource
在我的 webapplication 上下文中定义了一个,所以我可以将单个错误代码映射到一条消息。
But sometimes the default message will be best, and sometimes I expect that two of the error codes might have a relevant message, so we need to choose the best one.
但有时默认信息会是最好的,有时我预计错误代码中的两个可能有相关信息,所以我们需要选择最好的一个。
What method do I use to determine the bestpossible error message to present for a field error?
我使用什么方法来确定针对字段错误呈现的最佳错误消息?
- Do I need to write some algorithm to go through all the error codes and pick from the most specific?
- Does spring provide any support for helping determine the most specific error message?
- This whole process seems so long and convoluted, I thought spring was supposed to make this stuff easy. Maybe I'm totally off base somehow?
- 我是否需要编写一些算法来检查所有错误代码并从最具体的代码中进行选择?
- spring 是否提供任何支持来帮助确定最具体的错误消息?
- 整个过程看起来如此漫长而复杂,我认为春天应该让这些事情变得简单。也许我不知何故完全不在基地?
回答by Affe
You are, as you guessed, making it way harder on yourself than it needs to be. The FieldError
object is itself a MessageSourceResolvable
. You don't need to get the codes off of it then take individual codes manually to your message source and go looking. You can just pass it to your MessageSource
and it will find the most specific one that has a translation defined in your locale. (assuming your code resolver put them on in the right order.)
正如您所猜测的那样,您让自己变得比需要的更难。该FieldError
对象本身就是一个MessageSourceResolvable
。您无需从中获取代码,然后手动将各个代码带到您的消息源并进行查找。您可以将它传递给您的MessageSource
,它会找到在您的语言环境中定义了翻译的最具体的一个。(假设您的代码解析器以正确的顺序放置它们。)
You really don't even need to do that in most cases though. Putting the Errors
on your backing object and translating them yourself isn't usually needed. The form
namespace in the jsp library provides a tag that looks up error messages for you. All you need to do is put the Errors
in the ModelMap
. See docs:
不过,在大多数情况下,您甚至不需要这样做。Errors
通常不需要将放在您的支持对象上并自己翻译它们。form
jsp 库中的命名空间提供了一个标记,可以为您查找错误消息。所有你需要做的就是把Errors
在ModelMap
。查看文档: