java 在 Spring 中验证数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2100590/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:26:59  来源:igfitidea点击:

Validate Numbers in Spring

javavalidationspringspring-mvc

提问by Javi

I have a basic doubt in how to proceed in my application. I have a form and I need to validate that all the inputs which are numbers. I have a problem in deciding the type of the attributes of the bean asociated with the form. I don't know if setting them to String or to double and here are the reasons:

我对如何继续我的申请有一个基本的疑问。我有一个表格,我需要验证所有输入都是数字。我在决定与表单关联的 bean 的属性类型时遇到问题。我不知道是将它们设置为 String 还是 double ,原因如下:

  • If I set them to double: If I enter in the input something which is not a number when spring populates the inputs into the bean I get an Exception in the JSP that it could not convert it into double.
  • If I set them to String: I have a good validation although I have to change them later to double. But my problem here is that this bean is stored in a database with hibernate and the annotation @column would store it as a text and I would like to store it as if it were a double. Is there any posibility to change the column type to the double deferred type?
  • 如果我将它们设置为双倍:如果在 spring 将输入填充到 bean 中时我在输入中输入了一些不是数字的东西,我会在 JSP 中得到一个异常,它无法将其转换为双精度。
  • 如果我将它们设置为 String:我有一个很好的验证,尽管我稍后必须将它们更改为双倍。但我的问题是这个 bean 存储在一个带有休眠的数据库中,注释@column 会将它存储为文本,我想将它存储为双精度。是否有可能将列类型更改为双延迟类型?

Does anyone can give me any idea in how to preceed in this case? Thanks.

在这种情况下,有没有人可以给我任何想法?谢谢。

回答by

I suggest you always work with your domain types and not use String just because that's the way HTTP sends params. If a field has type double, you will use it as such in your code and also store it as such in the database. Let Spring convert the request params to your needed type.

我建议你总是使用你的域类型而不是使用 String 仅仅因为这是 HTTP 发送参数的方式。如果字段的类型为 double,您将在代码中使用它,并将其存储在数据库中。让 Spring 将请求参数转换为您需要的类型。

Data binding is useful for allowing user input to be dynamically bound to the domain model of an application (or whatever objects you use to process user input). Spring provides the so-called DataBinderclass to do exactly that.

数据绑定对于允许用户输入动态绑定到应用程序的域模型(或用于处理用户输入的任何对象)非常有用。Spring 提供了所谓的DataBinder类来做到这一点。

You can register those in the initBindermethod of your controllers and will allow you to transform the Strings from your request into the desired type. See for example the CustomNumberEditorclass used to parse user-entered number strings into Number properties of beans. You can then combine this with the Validator interfacefor more complex checks.

您可以在initBinder控制器的方法中注册它们,并允许您将请求中的字符串转换为所需的类型。例如,参见用于将用户输入的数字字符串解析为 bean 的 Number 属性的CustomNumberEditor类。然后,您可以将其与Validator 接口结合起来进行更复杂的检查。

EDIT:Spring binding uses typeMismatcherror codes for binding errors when a conversion fails (requiredcode if you specify a field as required but you don't supply it). In your case it defaults to showing the exception message. To change the message to a more friendly one, you must supply a bundle key in your property file using the typeMismatchprefix.

编辑:typeMismatch当转换失败时,Spring 绑定使用错误代码来绑定错误(required代码,如果您根据需要指定字段但不提供它)。在您的情况下,它默认显示异常消息。要将消息更改为更友好的消息,您必须使用typeMismatch前缀在属性文件中提供捆绑键。

This is specified by the DataBinder.setMessageCodesResolverand defaults to org.springframework.validation.DefaultMessageCodesResolver. In the javadocof DefaultMessageCodesResolveryou can see complete examples, but basically, you just have to add an entry like this in your properties file:

这是由 指定的DataBinder.setMessageCodesResolver,默认为org.springframework.validation.DefaultMessageCodesResolver。在DefaultMessageCodesResolverjavadoc 中,您可以看到完整的示例,但基本上,您只需在属性文件中添加这样的条目:

typeMismatch.yourField=Your user friendly error message goes here

回答by Raghuram

You can map the exception to the custom message if you have an entry in the following form in your message.properties (or the equivalent message bundle that you are using).

如果您的 message.properties(或您正在使用的等效消息包)中有以下形式的条目,则可以将异常映射到自定义消息。

typeMismatch.fieldName, where fieldName would be the name of the field you are validating.

typeMismatch.fieldName,其中 fieldName 将是您正在验证的字段的名称。

回答by Narayan

If you are using Spring 3.0

如果您使用的是 Spring 3.0

have a look at the Overriding Defaults with Annotationspart of

看看Overriding Defaults with Annotations部分

Spring 3 Type Conversion and Validation

Spring 3 类型转换和验证

If you are using Spring 2.x+ you can achieve this by registering Custom PropertyEditor as mentioned in above post

如果您使用的是 Spring 2.x+,您可以通过注册上面帖子中提到的自定义 PropertyEditor 来实现这一点