How to bind IP address to a Spring 3 @ModelAttribute?
Here's how my method looks like:
@RequestM开发者_如何转开发apping(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";
}
So, I need to bind the IP address to the Foo
object probably by calling getRemoteAddr()
on HttpServletRequest
. I've tried creating CustomEditor
for Foo
, but it doesn't seem to be the proper way. @InitBinder
looks more promising but I've yet to found out how.
The IP address is mandatory on the object and Spring combined with JSR-303 bean validation will give a validation error unless it is there.
What would be the most elegant way to solve this?
You can use @ModelAttribute
-annotated method to prepopulate the object with IP address:
@ModelAttribute("foo")
public Foo getFoo(HttpServletRequest request) {
Foo foo = new Foo();
foo.setIp(request.getRemoteAddr());
return foo;
}
@InitBinder("foo")
public void initBinder(WebDataBinder binder) {
binder.setDisallowedFields("ip"); // Don't allow user to override the value
}
EDIT: There is a way to do it using @InitBinder
only:
@InitBinder("foo")
public void initBinder(WebDataBinder binder, HttpServletRequest request) {
binder.setDisallowedFields("ip"); // Don't allow user to override the value
((Foo) binder.getTarget()).setIp(request.getRemoteAddr());
}
精彩评论