ConversionService in Spring

后端 未结 2 999
盖世英雄少女心
盖世英雄少女心 2020-12-08 02:57

I\'m following this scheme in a Spring application.

  1. Request is sent to the server with the id of the object and some other params to be populated in this objec
2条回答
  •  粉色の甜心
    2020-12-08 03:47

    I did exactly what Gary is saying above and it worked:

    I want to add some more information to the solution. As per the Spring documentation here a URI template variable gets translated to the target object using the Converter/ConversionService. I tried to use a @RequestParam("id") @ModelAttribute("contact") Contact contact, but I was getting an IllegalStateException: Neither BindingResult nor plain target object for bean name 'contact' available as request attribute for not having the model object contact in my view edit.jsp. This can be easily resolved by declaring a Model model and model.addAttribute(contact);. However, there is an even better way; using the URI template variable. It's strange why @RequestParam did not work.

    DID NOT WORK

    @RequestMapping("edit") //Passing id as .. edit?id=1
    public String editWithConverter(@RequestParam("id") @ModelAttribute("contact") Contact contact){
        logger.info("edit with converter");
         return "contact/edit";
    }
    

    WHAT WORKED

    @RequestMapping("edit/{contact}") //Passing id as .. edit/1
    public String editWithConverter(@PathVariable("contact") @ModelAttribute("contact") Contact contact){ // STS gave a warning for using {contact} without @PathVariable 
        logger.info("edit with converter");
         return "contact/edit";
    }
    

    So what does this thing do .. a link like ...edit/1 implicitly invokes the converter for String '1' to Contact of id '1' conversion, and brings this contact object to the view. No need for @InitBinder and since its a Converter registered with the ConversionService I can use this anywhere I want - implicitly or explicitly.

提交回复
热议问题