In the processing function of the controller, it is possible to encounter a situation where the parameters of the request have the same member:
@RequestMapping(value = "path",method = RequestMethod.POST)
public Map<String,Object> addPasswordInformation(A a,B b){
// do something here
}
If both A
and B
have the same member c
, the corresponding values cannot be automatically bound.
Solution#
Use the InitBinder
annotation.
Define binder methods#
@InitBinder("a")
public void initUserBinder(WebDataBinder webDataBinder){
webDataBinder.setFieldDefaultPrefix("classA.");
}
@InitBinder("b")
public void initUserBinder(WebDataBinder webDataBinder){
webDataBinder.setFieldDefaultPrefix("classB.");
}
Add annotations to the parameters of the controller method#
@RequestMapping(value = "path",method = RequestMethod.POST)
public Map<String,Object> addPasswordInformation(@ModelAttribute("a") A a,
@ModelAttribute("b") B b){
// do something here
}
The parameter of the @ModelAttribute("..")
annotation should be the same as the parameter of the @InitBinder("..")
annotation.
Modify the request data#
data = {
'classA.c':'asdasdasd',
,
'classB.c':'asdasdasd',
}
The above is Python code, but it is the same. Just change the key to include the prefix.