我有一个控制器工作正常,它可以注册和更新一个实体,以下是如何创建表格分别保存和更新实体
@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
}
我已阅读以下内容:
提到的链接围绕不同的entities
工作,例如User
和Customer
,它通过@InitBinder's value
属性,但我需要与同一个实体合作,我如何可以配置@InitBinder's value
以指示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){
…
}
答案 0 :(得分:12)
根据javadoc page for @InitBinder
,您可以在一个控制器中使用多个init-binder,并使用将应用它们的模型属性变量的名称对它们进行专门化。示例:
@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){
…
}