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

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

<p>and the other:</p>

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

      return "account";  
     }  
</code></pre>

<p>But I have another case: I have two <code>JSP</code>pages:
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).</p>

<p>I've tested this code:</p>

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

<pre><code> @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";  
     }      
</code></pre>

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

<pre><code>     @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";

}
</code></pre>

<p>I guess that the problem is that the 2 controllers are declared  with a method <code>method = RequestMethod.POST</code> when it should be one <code>GET</code> and an other <code>POST</code>. 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 <code>GET</code>, the insertion of the database fails!</p>

<p>Need your help please :( </p>