I'm looking for a way to customize the default Spring MVC parameter binding. Take this method as an example:
@RequestMapping(value="/index.html")
public ModelAndView doIndex(@RequestParam String param) {
...
This is easy, when I have just a String
that I want to extract from the request. However, I want to populate a more complete object, so that my method looks like this:
@RequestMapping(value="/index.html")
public ModelAndView doIndex(Foo bar) {
...
What I'm looking for is some way to declare a binding like this;
@RequestMapping(value="/index.html")
public ModelAndView doIndex(@FooPopulator Foo bar) {
...
And have some other kind of implementor (determined by the @FooPopulator
annotation) that does this:
public void doBind(Foo target, ServletRequest originalRequest) {
target.setX(this.computeStuffBasedOn(originalRequest));
target.sety(y);
}
So far I've found out about the @InitBinder
binder annotaion but I'm unsure whether that's really the right choice for this scenarion.
What's the best way?
just a quick thank you and the info, that I've found the "correct" solution to the problem. Spring already provides the WebArgumentResolver
for this scenario.
http://sergialmar.wordpress.com/2011/03/29/extending-handler-method-argument-resolution-in-spring-mvc/
http://scottfrederick.blogspot.com/2011/03/customizing-spring-3-mvcannotation.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With