aspose file tools*
The moose likes Spring and the fly likes Not able to display error messages. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Not able to display error messages." Watch "Not able to display error messages." New topic
Author

Not able to display error messages.

chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

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.

Thank you all in advance. Good day.


Love all, trust a few, do wrong to none.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

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
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

age is a primitive int (it can't be null) change it to Integer


[How To Ask Questions][Read before you PM me]
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Bill Gorder wrote:age is a primitive int (it can't be null) change it to Integer
Hi Mr. Bill, I made the changes. However it is working fine with the primitive datatype also. Using Integer is a good practice I know. Thanks for pointing it.

But what about by question? Can you please give any solution?
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

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?
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

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

Failed to convert property value of type java.lang.String to required type java.lang.Integer for property age; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value "abc" from type java.lang.String to type java.lang.Integer; nested exception is java.lang.IllegalArgumentException: Unable to parse abc

I am not able to see any exception in my console also.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Okay if @NumberFormat cannot be used to validate integers then which annotation should I use to check whether the entered input is a number or not?
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

I searched a lot for other alternatives. In a forum at here http://stackoverflow.com/questions/7997945/spring-mvc-annotation-validation-integer a poster said that use typeMispatch in messages.properrties. But din't mention anything about number validation.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Ok now I understood about what spring does. First spring tries to do data binding then validation. So it tries to set age with a string value which causes this error.

In most of the threads here

http://forum.springsource.org/showthread.php?87140-Spring-validation&p=292617#post292617
http://forum.springsource.org/showthread.php?87762-Validation-Empty-int-field

posters say to use properties file and declare a key with typeMismatch to let spring display a custom message instead of displaying the actual exception.

So the other alternative I am thinking is to declare age as String and do validation later. But I know one don't want to declare String for integers or doubles.

So I would like to follow the above approach which most of the posters suggest.

Is there any problem with this approach?
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

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:

http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html#context-functionality-messagesource

As far as how to define your codes read the API for DefaultMessageCodesResolver
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Not able to display error messages.