File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes How to send error messages? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "How to send error messages?" Watch "How to send error messages?" New topic
Author

How to send error messages?

chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Hi all, I am making an ajax call to a controller. I am also sending some post data. I am validating the data using annotations. At last I am having a BindingResult object which has the list of possible errors. I hav to send the error list to the client.

I am extracting all the errors at server side like this:Is this a good approach? Or is there any other approach of extracting all errors from BindingResult object?

Thank you all in advance. Good day.


Love all, trust a few, do wrong to none.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1649
    
    7

If your goal is just to extract errors from the binding result what you have looks like it would work, it might be a little easier to read without the iterator



Also this works if you just want the default message if you wanted to resolve this against messages defined in your MessageSource you would need something like




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

Joined: Sep 15, 2009
Posts: 806

Thank you Mr. Bill. I am using messageSource.getMessage(fieldError, null); now.

I have one last doubt. BindingResult is creating a dependency on spring framework. Is this a good approach?
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1649
    
    7

Can you clarify what you mean by

BindingResult is creating a dependency on spring framework


Your passing in the BindingResult as a method parameter following one of your model attributes correct?
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

This is my method signature in the controller
PS: No

What I meant actually is BindingResult is a spring framework class and few say that not to use spring specific classes in your code, do not implement spring specific interfaces or extend any class because it will become hard to migrate the code.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1649
    
    7

In that case I would say No you are not creating a dependency on the Spring Framework. You are not implementing any Spring interfaces or worse extending any Spring classes. You are simply using the framework which is exactly the purpose for which it was intended.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

How come passing BingindResult as a method parameter not create a dependency? Can you please explain?
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Okay no offense. I think there are will be some minimal dependencies on spring framework without which coding will be difficult. For example I can eliminate the use of BindingResult. But in this case we have to validate the data our self and push the errors into some user defined object.

In this case I hope I cannot make use of @Valid annotation also. So I am thinking that we cannot write quickly without having some minimal dependencies.

Thank you Mr. Bill. Please point me if I am assuming is wrong.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1649
    
    7

Ok maybe I should explain differently. You have a dependency on it in the sense you have to import the correct package just like if you use List from the java API. However I am speaking more about dependency from a coupling perspective. When a programmer is forced to extend framework classes it would be said the framework is more invasive and tightly coupled to your application. One of the Spring Frameworks core principals is loose coupling and not being invasive into the developers code.

Also BindingResult is simply an interface. Interfaces are another strategy to promote loose coupling. When you have a class that does something like this:



You can say that there is a higher coupling in this case then say this:



Here List is an interface and further more we have reduced coupling by allowing Spring to autowire in an implementation for this. We don't care if it is a ArrayList or a LinkedList etc. And we can change out that implementation without touching this class at all. Therefore we would say this is loosly coupled.

There will always be some degree of coupling that is impossible to avoid but the goal is to make this as minimal as possible.

BindingResult just like List is simply there to make your life easier as a developer.


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to send error messages?