This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi all, I am trying to validate a user registration form. I am able to validate and redirect to appropriate view. When user enters invalid data I am forwarding the request to the same registration page. I am not able to display the error messages. Can anyone please help me. This is my codeIn User.javaregister.jspFor now I dint create any message.properties file. Anyhow some default error message will be displayed. What is wrong in my code?
Can anyone please tell me if I am missing something.
Hi all, in the jsp when I am using user.name instead of NotEmpty.user.name I am able to see the error messages.
Now the problem is it is displaying a default message. I want to have my own message to be displayed. So I am using message attribute and able to display my own custom message like this
But when name is empty it is saying "Name cannot be empty" and "Name size can be between 10 and 100". How can I make it display only one message? Should I remove message attribute for @NotEmpty?
Another problem I am facing is I introduced a new text field age in the view and appropriate filed in the User class like thisWhen I enter nothing in the age field it is displaying "Property age threw exception; nested exception is java.lang.IllegalArgumentException". How can add my custom message here? There is no message attribute for @NumberFormat annotation. And it is not displaying "Age cannot be empty" error message when I am not enter any value in the age field.
How can I fix this? Thank you all in advance. Good day
When I enter nothing in the age field it is displaying "Property age threw exception; nested exception is java.lang.IllegalArgumentException". How can add my custom message here?
This is an exception not part of validation, are you still getting this exception after you changed it to Integer? If so post the code you are executing to get this as well as the full stack trace.
There is no message attribute for @NumberFormat annotation.
Nope as I said before this is not a validation annotation. It is a Spring annotation for formatting numbers, not validating them. My guess is you are not using it and it can just be removed.
And it is not displaying "Age cannot be empty" error message when I am not enter any value in the age field.
My guess is it was throwing the IllegalArgumentException when you were trying to assign null or empty string to a primitive int. I would think that should work now. Did you change the type to Integer on your getters and setters too?
This is the codeI am not getting any exception stack trace in my console. So I thought that @NumberFormat is used for validation. This is my controller codeWhen I leave the age field empty this time it is saying "Age cannot be empty". However when I try to enter a string for age I am getting
Looks like you figured it out on your own You are absolutely correct binding happens before validation and in this case you had a binding failure which by default displays the exception. The correct way to handle this is not to change it to a string but to make the message a more fitting one. It is already an error on your binding result you just need to customize the message. For this you need to declare a message source in addition to the properties file. You can read up on how to do that here: