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?
This is my method signature in the controller
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.
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.
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.
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.