title: How to store data is session scope in Spring
body: <p>I have created two object in session scope like this <code>@SessionAttributes({"userObj","simpleValue"})</code> in my controller.</p>
  
  <p>I am adding a user object and String at these variables in my controller like this:</p>
  
  <pre><code>modelAndView.addObject("userObj", user);
  modelAndView.addObject("simpleValue", "Hello World");
  </code></pre>
  
  <p><code>User</code> class is a simple class with 2 properties <code>id</code> &amp; <code>name</code>.</p>
  
  <p>Lets say I created this in a controller called <code>Controller1</code> which shows <code>Page1</code>. I can see the data of my session variables in <code>page1</code>.</p>
  
  <p>Now I created another controller say <code>Controller2</code> (this controller has no relation with <code>page1</code> or <code>Controller1</code>) which displays <code>page2</code>, now in this new page I am able to access only the single session attribute for <code>simpleValue</code>, I am not able to access <code>userObj</code>, I am getting empty result.</p>
  
  <p>As per the <a href="http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/SessionAttributes.html" rel="nofollow">@SessionAttributes</a>, it says:</p>
  
  <blockquote>
    <p>NOTE: Session attributes as indicated using this annotation correspond
    to a specific handler's model attributes, getting transparently stored
    in a conversational session. Those attributes will be removed once the
    handler indicates completion of its conversational session. Therefore,
    use this facility for such conversational attributes which are
    supposed to be stored in the session temporarily during the course of
    a specific handler's conversation.</p>
  </blockquote>
  
  <p>So I have 2 questions here:</p>
  
  <p>1) I am not able to understand why Spring allowing me to access the simple property but not the User object in <code>page2</code>.</p>
  
  <p>2) The document also says we have to use either traditional <code>session</code> or <code>WebRequest</code>. I able to use the <code>sessison</code> and access the variables, but can someone please help me how to use <code>WebRequest</code> for storing objects in session?</p>
  
  <p>This is the code that I am using:</p>
  
  <p><strong>Controller1.java</strong></p>
  
  <pre><code>@Controller
  @SessionAttributes({"mySessionAttr","userObj"})
  public class Controller1 {
  
      @RequestMapping(value="/page1")
      public ModelAndView singleFieldPage() {
          ModelAndView modelAndView = new ModelAndView();
          modelAndView.addObject("mySessionAttr", "Hello World");
          modelAndView.addObject("userObj", new User(1,"Scott"));
          modelAndView.setViewName("page1");
          return modelAndView;
      }
  }
  </code></pre>
  
  <p><strong>Controller2.java</strong></p>
  
  <pre><code>@Controller
  public class Controller2 {
  
      @RequestMapping(value="/page2")
      public ModelAndView singleFieldPage(HttpSession session) {
          return new ModelAndView("page2");
      }   
  }
  </code></pre>
  
  <p><strong>page1.jsp &amp; page2.jsp</strong>, both have same code.</p>
  
  <pre><code>&lt;p&gt;Session variable : ${simpleValue}&lt;/p&gt;
  &lt;p&gt;Product's name is ${userObj.name}. The id is ${userObj.id}
  </code></pre>
  
  <p>This is my <strong>User.java</strong>:</p>
  
  <p>public class User {</p>
  
  <pre><code>private int id;
  private String name;
  
  public User() {
  }
  
  public User(int id, String name) {
      this.id = id;
      this.name = name;
  }
  
     // Setters &amp; Getters
  }
  </code></pre>
  
  <p>This is my configuration files:</p>
  
  <p><strong>Spring based configuration</strong></p>
  
  <pre><code>@Configuration
  @ComponentScan("com.examples")
  @EnableWebMvc
  public class WebAppConfig {
  
      @Bean
      public UrlBasedViewResolver setupViewResolver() {
          UrlBasedViewResolver resolver = new UrlBasedViewResolver();
          resolver.setPrefix("/WEB-INF/pages/");
          resolver.setSuffix(".jsp");
          resolver.setViewClass(JstlView.class);
          return resolver;
      }
  
  }
  </code></pre>
  
  <p><strong>Web based configuration</strong></p>
  
  <pre><code>public class Initializer implements WebApplicationInitializer {
  
      public void onStartup(ServletContext servletContext) throws ServletException {
  
          AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
          ctx.register(WebAppConfig.class);
  
          ctx.setServletContext(servletContext);  
  
          Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
          servlet.addMapping("/");
          servlet.setLoadOnStartup(1);
  
      }
  
  }
  </code></pre>