PDA

View Full Version : catch binding exception


shoa
Jun 3rd, 2006, 08:02 PM
Hello

In my Validator, to catch a error, I can have a function such as:

if (student.getStudentName() ==null ||student.getStudentName().equals(""){
errors.rejectValues("studentName", "error", "Please enter a name");
}

It can be seen that if there is missing of student name, i can show a defined message "Please enter a name"

However, if I have Binding Exception for a field, how i can show a defined message - for example a NumberFormatException for a field ????

Thanks
sho

katentim
Jun 4th, 2006, 05:07 AM
Add this to your messages.properties:
typeMismatch.java.lang.NumberFormatException=must be a number
or
typeMismatch.myField=invalid number

There's an example in the PetClinic sample.

The constant is documented here:
http://static.springframework.org/spring/docs/1.2.x/api/constant-values.html#org.springframework.beans.TypeMismatch Exception.ERROR_CODE
and