title: Spring MVC : How to pass Model object from one method in controller to another method in same controller?
body: <p>I have integrated Spring Security in my application , and would like to display an error message to the user in case of Bad credentials.
  jsp:</p>
  
  <pre><code>    &lt;c:out value='${secerror}' /&gt; //prints nothing on screen
      &lt;c:if test="${empty secerror}"&gt;
      error is empty or null. //always prints this line on screen
      &lt;/c:if&gt;
      &lt;c:if test="${not empty secerror}"&gt;
      &lt;div class="errorblock"&gt;
          Your login attempt was not successful, try again.&lt;br /&gt; Caused :
          ${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message}
      &lt;/div&gt;
      &lt;/c:if&gt;
       &lt;c:set var = "url" value = "/j_spring_security_check" /&gt;
      &lt;form:form method="post" commandName="portalLogin" action="${pageContext.servletContext.contextPath}${url}" name="f"&gt;
  </code></pre>
  
  <p>[Update]: Sorry all, i realized that my Model object was getting overriden after i redirect to portalLogin.html as i had created a new model object created there previously.
  But i tried few easy options by which i can pass Model object from one controller method to another method in the same controller. But nothing worked.</p>
  
  <ol>
  <li>I tried using forward: prefix instead of redirect prefix. For this, i didn't get error message at all. </li>
  <li><p>I tried below code in loginerror method.</p>
  
  <pre><code>    return new ModelAndView("portalLogin","secerror","true");
  </code></pre></li>
  </ol>
  
  <p>I was getting following error for the above code:</p>
  
  <pre><code>        java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'portalLogin' available as request attribute
  </code></pre>
  
  <p>I did come across <a href="https://stackoverflow.com/questions/7429649/how-to-pass-model-attributes-from-one-spring-mvc-controller-to-another-controlle">this</a> link, but i found it to be a very lengthy solution and wasn't sure if iv'e to write that much code.</p>
  
  <p>I wasn't sure if i can use Model object for @ModelAttribute annotations<a href="http://www.coderanch.com/t/506713/Spring/pass-object-MVC-controllers" rel="nofollow noreferrer">@ModelAttribute</a>.</p>
  
  <p>Please provide me with code snippets / examples which i can try out.</p>
  
  <p>My controller method is like this:</p>
  
  <pre><code>    @RequestMapping("/portalLogin")
  public ModelAndView goToLogin(Model model) {
      try {
          System.out.println("Enter goToLogin************************");
      } catch (Exception exception) {
  
      }
      return new ModelAndView("portalLogin", "portalLogin", new LoginModel());
      //return new ModelAndView("portalLogin", model);
  }
  
      @RequestMapping(value="/loginfailed", method = RequestMethod.GET)
  public ModelAndView loginerror(ModelMap model) {
  
      //model.addAttribute("secerror", "true");
      //return "redirect:portalLogin.html";
      return new ModelAndView("portalLogin","secerror","true");
  
  }  
  </code></pre>
  
  <p>[Update]: As a work around i added goToLogin method logic inside loginerror method as my only intention is to get portalLogin page. Error was thrown as expected.</p>
  
  <pre><code>    @RequestMapping(value="/loginfailed", method = RequestMethod.GET)
  public ModelAndView loginerror(Model model) {
       model.addAttribute("secerror", "true");       
      return new ModelAndView("portalLogin", "portalLogin", new LoginModel());
      }
  </code></pre>
  
  <p>But still i would like to know if i can pass Model object from one controller method to another method in the same controller through some way.</p>