开发者

Validate Numbers in Spring

I have a basic doubt in how to开发者_如何学JAVA 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:

  • 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?

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


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.

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 DataBinder class to do exactly that.

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

EDIT: Spring binding uses typeMismatch error codes for binding errors when a conversion fails (required code 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 typeMismatch prefix.

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

typeMismatch.yourField=Your user friendly error message goes here


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).

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


If you are using Spring 3.0

have a look at the Overriding Defaults with Annotations part of

Spring 3 Type Conversion and Validation

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜