Spring MVC 自定义方法参数绑定 [英] Spring MVC customized method parameter binding

查看:31
本文介绍了Spring MVC 自定义方法参数绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种自定义默认 Spring MVC 参数绑定的方法.以这个方法为例:

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) {
  ...

这很容易,当我只有一个 String 想要从请求中提取时.但是,我想填充一个更完整的对象,以便我的方法如下所示:

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) {
  ...

还有一些其他类型的实现器(由 @FooPopulator 注释确定)来执行此操作:

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);
}

到目前为止,我已经了解了 @InitBinderbinder 注释,但我不确定这是否真的适合这种情况.

So far I've found out about the @InitBinderbinder annotaion but I'm unsure whether that's really the right choice for this scenarion.

最好的方法是什么?

推荐答案

感谢您和我找到了问题的正确"解决方案的信息.Spring 已经提供了 WebArgumentResolver 用于此场景.

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

这篇关于Spring MVC 自定义方法参数绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆