Search code examples
springhttp-redirectmodelcontrollertransfer

How to redirect from one controller method to another method with model data in spring boot application


In this example I am trying to redirect from handleSaveContact() controller method from contactSuccessMsg() controller method but after transfer I need to display success or update or failure msg to the UI which is only possible if I transfer Model data from 1st method to 2nd.

Could any one please suggest me how I can trasfer model data from one controller method to another controller method.

@GetMapping(value={"/", "/loadForm"})
    public String loadContactForm(Model model) {
        
        model.addAttribute("contact", new Contact());
        return "index";
    }
    
    
    @PostMapping("/saveContact")
    public String handleSaveContact(Contact contact, Model model) {
        String msgTxt = null;
        if(contact.getContactId()==null) {
            msgTxt = "Contact Saved Successfully..!!";
        }else {
            msgTxt = "Contact Updated Successfully..!!";
        }
        
        contact.setIsActive("Y");
        boolean isSaved = contactService.saveContact(contact);
        if(isSaved) {
            model.addAttribute("successMsg", msgTxt);
        }else {
            model.addAttribute("errorMsg", "Failed To Save Contact..!!");
        }
        return "redirect:/contactSuccessMsg";
    }
    
    
    /**
     * To resolve Double Posting problem, redirecting the post req method to get request.
     * @param contact
     * @param model
     * @return
     */
    @GetMapping(value="/contactSuccessMsg")
    public String contactSuccessMsg(Model model) {
        model.addAttribute("contact", new Contact());
            
        return "index";
    }

Solution

  • I used Spring 3.2.3

    1.)Added RedirectAttributes redirectAttributes to the method parameter list in controller1.

     public String controlMapping1(
            @ModelAttribute("mapping1Form") final Object mapping1FormObject,
            final BindingResult mapping1BindingResult,
            final Model model, 
            final RedirectAttributes redirectAttributes)
    
    1. Inside the method added code to add flash attribute to redirectAttributes

    redirectAttributes.addFlashAttribute("mapping1Form", mapping1FormObject);

    1. Then, in the second contoller use method parameter annotated with @ModelAttribute to access redirect Attributes :

    @ModelAttribute("mapping1Form") final Object mapping1FormObject

    Here is the sample code from Controller 1:

    @RequestMapping(value = { "/mapping1" }, method = RequestMethod.POST)
    public String controlMapping1(
            @ModelAttribute("mapping1Form") final Object mapping1FormObject,
            final BindingResult mapping1BindingResult,
            final Model model, 
            final RedirectAttributes redirectAttributes) {
    
        redirectAttributes.addFlashAttribute("mapping1Form", mapping1FormObject);
    
        return "redirect:mapping2";
    }  
    

    From Contoller 2:

    @RequestMapping(value = "/mapping2", method = RequestMethod.GET)
    public String controlMapping2(
            @ModelAttribute("mapping1Form") final Object mapping1FormObject,
            final BindingResult mapping1BindingResult,
            final Model model) {
    
        model.addAttribute("transformationForm", mapping1FormObject);
    
        return "new/view";  
    }