title: Access session scoped bean from request scoped bean
body: <p>I'm trying to use a pattern found on a IceFaces page.(I'm not using IceFaces, using PrimeFaces)</p>
  
  <p>In this case I have two beans:</p>
  
  <p><em>UserController</em>  and <em>Usermodel</em></p>
  
  <p>On my <em>UserModel</em> I have a instance of <em>UserVO</em> (created by another programmer).
  On My <em>UserController</em> I have this:</p>
  
  <pre><code>@ManagedBean
  @RequestScoped
  public class UserController implements Serializable
  {
  
      private static final long serialVersionUID = 1L;
      private UserBO bo;
      private UserModel model;
  
      public UserController()
      {
          bo = new UserBO();
          model = new UserModel();
      }
  
      public void Login() throws IOException
      {
          model.setUserVo(bo.executeLogin(model.getUserVo()));
          ExternalContext externalContent = FacesContext.getCurrentInstance().getExternalContext();
          if (!model.getUserVo().isError())
          {
              model.setLoggedIn(true);
              externalContent.getSessionMap().put("userSession", model);
              externalContent.redirect(externalContent.getRequestContextPath() + "/views/request/search.html");
          } else
          {
              model.setLoggedIn(false);
              FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, model.getUserVo().getMessage(), model.getUserVo().getLogin());
              FacesContext.getCurrentInstance().addMessage(null, facesMessage);
          }
      }
  
      public UserBO getBo()
      {
          return bo;
      }
  
      public void setBo(UserBO bo)
      {
          this.bo = bo;
      }
  
      public UserModel getModel()
      {
          return model;
      }
  
      public void setModel(UserModel model)
      {
          this.model = model;
      }
  }
  </code></pre>
  
  <p>As you can see, I create a new instance of <em>UserModel</em> and set it with what was returned from the <code>bo.executeLogin()</code> and it is working, my object is returned.</p>
  
  <p>To make sure the user is logged in, I have a property on my <em>UserModel</em>:</p>
  
  <pre><code>@ManagedBean
  @SessionScoped
  public class UserModel
  {
  
      private UserVO userVo;
      private Boolean loggedIn = false;
  
      public UserModel()
      {
          userVo = new UserVO();
      }
  
      public UserVO getUserVo()
      {
          return userVo;
      }
  
      public void setUserVo(UserVO userVo)
      {
          this.userVo = userVo;
      }
  
      public Boolean getLoggedIn()
      {
          return loggedIn;
      }
  
      public void setLoggedIn(Boolean loggedIn)
      {
          this.loggedIn = loggedIn;
      }
  </code></pre>
  
  <p>I have a <em>template.xhtml</em> with:</p>
  
  <pre><code>&lt;ui:fragment rendered="#{userModel.loggedIn}"&gt;
              &lt;ui:include src="../includes/top.xhtml"/&gt;
  &lt;/ui:fragment&gt;
  </code></pre>
  
  <p>And the thing is that it is not working, is not getting the <code>loggedIn</code> property value.</p>
  
  <p>My guess is that accessing this way I'm kinda creating a new instance of <em>UserModel</em>, if so, it is a problem because my <em>UserController</em>  is not session scoped, only the <em>UserModel</em></p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Instead of using this <code>loggedIn</code> property I know I can simply check if the <em>UserModel</em> <code>userVo</code> property is set but the problem is about the session scoped bean, I can't access it from <em>UserController</em>, where it is set because it isn't scoped session, and my <em>template.xhtml</em>  will be used by every page.</p>