3
这里是我的方法是如何的样子:如何将IP地址绑定到Spring 3 @ModelAttribute?
@RequestMapping(value = "/form", method = RequestMethod.POST)
public String create(@ModelAttribute("foo") @Valid final Foo foo,
final BindingResult result, final Model model) {
if (result.hasErrors())
return form(model);
fooService.store(foo);
return "redirect:/foo";
}
所以,我需要大概通过调用HttpServletRequest
getRemoteAddr()
到IP地址Foo
对象绑定。我已经尝试为Foo
创建CustomEditor
,但它似乎不是正确的方法。 @InitBinder
看起来更有希望,但我还没有找到如何。
该对象上的IP地址是强制性的,Spring与JSR-303 bean验证组合会产生验证错误,除非它存在。
什么是最优雅的方式来解决这个问题?
非常感谢,我从来没有在Spring文档中看到过这样的例子。我选择了第一种方式,因为纯粹的'@ InitBinder'方式在铸造方面看起来有些笨拙。 – hleinone 2010-02-24 22:34:45