Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC customized method parameter binding

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 Stringthat 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 @InitBinderbinder annotaion but I'm unsure whether that's really the right choice for this scenarion.

What's the best way?

like image 655
Christian Seifert Avatar asked Jun 29 '11 10:06

Christian Seifert


1 Answers

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

like image 54
Christian Seifert Avatar answered Oct 21 '22 16:10

Christian Seifert