Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass object between two controllers in spring mvc?

Actually I've googled to see how we can pass data from one controller to another in Spring MVC and I found that using flash attributes do the job. It consists on declaring one controller with RequestMapping=GET and an other with RequestMapping=POST. I have tested this code in my controller and it worked.

 @RequestMapping(value ="/ajouter", method = RequestMethod.POST)  
     public String add(@ModelAttribute User user,final RedirectAttributes redirectAttributes) {  
         redirectAttributes.addFlashAttribute("user", user);
      return "redirect:/account";  
     }  

and the other:

 @RequestMapping(value ="/account", method = RequestMethod.GET)  
     public String addAccount(@ModelAttribute("user") User user,@ModelAttribute Account account) { 
                System.out.println(user.getId());

      return "account";  
     }  

But I have another case: I have two JSPpages: The first will add a user into the database. I want to recuperate the id of the user just inserted to set it as a foreign key for the account (after submitting the page for adding a user, a second page for adding an account appears).

I've tested this code:

This controller will insert a user into the database and recuperate the user which has been just inserted.

 @RequestMapping(value ="/addUser", method = RequestMethod.POST)  
     public String add(@ModelAttribute User user,final RedirectAttributes redirectAttributes) {  
        this.userService.insertData(user);
         redirectAttributes.addFlashAttribute("user", user);
      return "redirect:/account";  
     }      

and this controller will insert an account into the database, but can not recuperate the user id of the user just inserted.

     @RequestMapping(value= "/ajouterCompte", method = RequestMethod.POST)
public String addCompte(@ModelAttribute("account") Account compte,@ModelAttribute("user") User user){
    System.out.println(user.getId());  //this code shows 0

    System.out.println(compte.getPwd());

     String hashPassword = passwordEncoder.encode(compte.getPwd());
     System.out.println(hashPassword);
     compte.setPwd(hashPassword);
    compte.setId(user.getId()); 
       this.compteService.insertCompte(compte);

    return "redirect:/users";

}

I guess that the problem is that the 2 controllers are declared with a method method = RequestMethod.POST when it should be one GET and an other POST. But in this case how can I recuperate the id of the user so that it could be inserted into the database? When I declare the second controller with GET, the insertion of the database fails!

Need your help please :(

like image 279
pietà Avatar asked Oct 19 '22 08:10

pietà


1 Answers

The object which are set as flash attributes are only available for the first request after those have been set. (As also explained in the reference guide).

Assuming that in your account controller you have a GET based method for /account pre-populate the Account with the information at that point.

public String account(@ModelAttribute("user") User user, Model model) {
    Account account = new Account();
    account.setId(user.getId());
    model.addAttribute("account", account);
    return "account";
}

Now you can either annotate your controller with @SessionAttribute("account") to have the Account stored in the session in between requests. If so then you have to modify your POST handling method to include a SessionStatus object and after storing call setComplete() on it.

public String addCompte(@ModelAttribute("account") Account compete, SessionStatus sessionStatus){
    ...
    sessionStatus.setComplete();
    return "redirect:/users";
}

Or store the pre-filled information in hidden form fields so that it gets passed along with the following request and the Account can be reconstructed.

<form:hidden path="id" />
like image 135
M. Deinum Avatar answered Nov 15 '22 06:11

M. Deinum