views:

368

answers:

3

Hello, 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:

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

+1  A: 

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
dpb
thanks, but I have a question about this. I create a CustomNumberEditor, but when I enter a String which is not a number I must throw an Exception (I can't just set it to 0). Then when I perform validation in the errors it finds out that there is this exception but when I try to write the error message with form:errors tag it writes in the screen the Exception cause. Could I map this exception to a custom message making possible not to show the Exception Class name? thanks.
Javi
@Javi: I edited the answer to respond to your question.
dpb
Thank you very much
Javi
A: 

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

Narayan
A: 

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.

Raghuram