title: Spring MVC passing data between controllers
body: <p>I'm writing web application using SpringMVC. I would like to keep some data in session until user logout. Earlier I did it by <code>@SessionAttributes("someData")</code>, but then I had to put <code>@ModelAttribute("someData") someDataType someData</code> as argument for every request mapping method. So this is what I did :</p>
  
  <p>My AccessData class :</p>
  
  <pre><code>@Component
  @Scope(value = "session")
  public class AccessData {
  
      private long userID;
  
      private String userKey;
  
      public AccessData(long ID, String key) {
          this.userID = ID;
          this.userKey = key;
      }
  
      public long getUserID() {
          return userID;
      }
  
      public void setUserID(long userID) {
          this.userID = userID;
      }
  
      public String getUserKey() {
          return userKey;
      }
  
      public void setUserKey(String userKey) {
          this.userKey = userKey;
      }
  }
  </code></pre>
  
  <p>First controller(here I'm getting and validating user input from form) :</p>
  
  <pre><code>@Controller
  @Scope(value = "session")
  public class LoginController {
  
      @ModelAttribute("accessData")
      public AccessData getAccessData() {
          return this.accessData;
      }
  
      private Utils utilsService;
  
      private LoginService loginService;
  
      @Autowired
      private AccessData accessData;
  
      @Autowired
      public LoginController(LoginService loginService, Utils utils) {
          this.loginService = loginService;
          this.utilsService = utils;
      }
  
      @RequestMapping(value = ControllerPaths.LOGIN, method = RequestMethod.POST)
      public ModelAndView showLoginStatus(
              @ModelAttribute(LOGINDATA) LoginData loginData) {
          try {
              accessData = loginService.validateLoginData(loginData);
          } catch (IncorrectLoginDataException e) {
              logger.trace("Showing fail login screen...");
              return utilsService.getShowView(ViewPaths.LOGIN_FAIL);
          } catch (HTTPException e) {
              return utilsService.getShowViewWithStringAttribute(
                      ViewPaths.INFO_VIEW, MESSAGE, CONNECTION_ERROR);
          }
          return utilsService.getShowView(ViewPaths.LOGIN_SUCCESS);
      }
  }
  </code></pre>
  
  <p>Second controller :</p>
  
  <pre><code>@Controller
  @Scope(value = "session")
  public class SecondController {
  
      @ModelAttribute("accessData")
      public AccessData getAccessData() {
          return this.accessData;
      }
  
      private Utils utilsService;
  
      private LoginService loginService;
  
      @Autowired
      private AccessData accessData;
  
      @Autowired
      public SecondController(LoginService loginService, Utils utils) {
          this.loginService = loginService;
          this.utilsService = utils;
      }
  
      @RequestMapping(value = ControllerPaths.SHOW_ACCESS_DATA, method =   RequestMethod.GET)
      public ModelAndView showAccessData(
          System.out.println(accessData.getUserKey());
          return utilsService.getShowView(ViewPaths.INDEX);
      }
  }
  </code></pre>
  
  <p>The problem is that when I'm printing userKey value in second controller, the value is null. I checked if I'm getting correct data from server in <code>LoginController</code> and it's ok. So what am I doing wrong? Thanks in advance for help</p>