spring – 配置两个@InitBinder以使用相同的Model或Entity,但是针对不同的@RequestMappings
作者:互联网
我有一个控制器工作正常,它可以注册和更新一个实体,以及如何分别创建表格以分别保存和更新实体
@RequestMapping(value="/registrar.htm", method=RequestMethod.GET)
public String crearRegistrarFormulario(Model model){
…
}
@RequestMapping(value="/{id}/actualizar.htm", method=RequestMethod.GET)
public String crearActualizarFormulario(@PathVariable("id") String id, Model model){
…
}
直到这里我没有问题.
我的问题是关于@InitBinder
我需要与同一个实体Deportista(运动员)一起工作,这是一个用于保存和更新的特殊设置.例如
@InitBinder
public void registrarInitBinder(WebDataBinder binder) { // register or save
logger.info(">>>>>>>> registrarInitBinder >>>>>>>>>>>>>");
…
CustomDateEditor customDateEditor = new CustomDateEditor(...
…
}
@InitBinder
public void actualizarInitBinder(WebDataBinder binder) { // update
logger.info(">>>>>>>> actualizarInitBinder >>>>>>>>>>>>>");
…
CustomDateEditor customDateEditor = new CustomDateEditor(...
…
binder.setDisallowedFields(…) //I need this only for update
}
我看过以下内容:
> How to define multiple initBinders
> Addiing multiple validators using initBinder
提到的链接围绕不同的实体工作,例如User和Customer,它通过@ InitBinder的value属性,但是我需要使用相同的实体,我如何配置@ InitBinder的值来指示Spring使用或区分每个@InitBinder?一个用于保存和更新.
谢谢
编辑:从Serge Ballesta的回答中,以下内容也是强制性的:
@Controller
@RequestMapping(value="/deportista")
@SessionAttributes(value={"deportistaRegistrar", "deportistaActualizar"})
public class DeportistaController {
…
@RequestMapping(value="/registrar.htm", method=RequestMethod.GET)
public String crearRegistrarFormulario(Model model){
Deportista deportista = new Deportista();
model.addAttribute("deportistaRegistrar", deportista);
return "deportista.formulario.registro";
}
@RequestMapping(value="/{id}/actualizar.htm", method=RequestMethod.GET)
public String crearActualizarFormulario(@PathVariable("id") String id, Model model){
Deportista deportista = this.fakeMultipleRepository.findDeportista(id);
model.addAttribute("deportistaActualizar", deportista);
return "deportista.formulario.actualizacion";
}
它让他的答案工作:
// registrarInitBinder will be used here
@RequestMapping(value="/registrar.htm", method=RequestMethod.POST)
public String doCrearRegistrarFormulario(@ModelAttribute("deportistaRegistrar") XXX value,
BindingResult result, Model model){
…
}
// actualizarInitBinder will be used here
@RequestMapping(value="/{id}/actualizar.htm", method=RequestMethod.POST)
public String crearActualizarFormulario(@PathVariable("id") String id,
@ModelAttribute("deportistaActualizar") XXX value, BindingResult result, Model model){
…
}
解决方法:
根据javadoc page for @InitBinder
,您可以在一个控制器中使用多个init-binder,并使用将要应用它们的model属性变量的名称对它们进行特化.示例:
@InitBinder("saveValue")
public void registrarInitBinder(WebDataBinder binder) { // register or save
logger.info(">>>>>>>> registrarInitBinder >>>>>>>>>>>>>");
…
CustomDateEditor customDateEditor = new CustomDateEditor(...
…
}
@InitBinder("updateValue")
public void actualizarInitBinder(WebDataBinder binder) { // update
logger.info(">>>>>>>> actualizarInitBinder >>>>>>>>>>>>>");
…
CustomDateEditor customDateEditor = new CustomDateEditor(...
…
binder.setDisallowedFields(…) //I need this only for update
}
然后(XXX是将由提交处理的表单对象的类型)
// registrarInitBinder will be used here
@RequestMapping(value="/registrar.htm", method=RequestMethod.POST)
public String doCrearRegistrarFormulario(@ModelAttribute("saveValue") XXX value,
BindingResult result, Model model){
…
}
// actualizarInitBinder will be used here
@RequestMapping(value="/{id}/actualizar.htm", method=RequestMethod.POST)
public String crearActualizarFormulario(@PathVariable("id") String id,
@ModelAttribute("updateValue") XXX value, BindingResult result, Model model){
…
}
标签:spring-mvc,spring,spring-3,spring-4,spring-mvc-initbinders 来源: https://codeday.me/bug/20190715/1466913.html