问题描述
我正在寻找一种自定义默认的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 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) {
...
还有一些其他类型的实现者(由 @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);
}
到目前为止,我发现了 @
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.
最好的方法是什么?
推荐答案
只是一个快速的谢谢和信息,我找到了正确的解决方案的问题。 Spring已经提供了。
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.
这篇关于Spring MVC定制方法参数绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!