配置两个@InitBinder以使用相同的模型或实体,但是针对不同的@RequestMappings

时间:2014-09-16 15:56:54

标签: spring spring-mvc spring-3 spring-4 spring-mvc-initbinders

我有一个控制器工作正常,它可以注册和更新一个实体,以下是如何创建表格分别保存和更新实体

@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工作,例如UserCustomer,它通过@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){
  …
}

1 个答案:

答案 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){
  …
}