title: JSF - App design issue. How to get data in backing bean A, after user logs in to backing bean B
body: <p>I'm trying to load some data after a user logs in to my app. I need the username to load data specific to the user. The problem I'm having is that I have a <code>SessionScoped</code> backing bean that contains the code to log the user in, and then I have a <code>ViewScoped</code> page specific backing bean (which injects the SessionScoped bean) which is to load the data for the page. </p>
  
  <p>Currently this is the code that I have and I get a null pointer when loading the data because the username doesn't exist when the <code>loadData()</code> method is called.</p>
  
  <p>I'm having a hard time coming up with a solution to this problem as I'd like to NOT have to put the login dialog on every page and keep it in the template.xhtml file if possible.</p>
  
  <p>template.xhtml</p>
  
  <pre class="lang-xml prettyprint-override"><code>...
  &lt;!-- Login Dialog --&gt;
      &lt;p:dialog id="loginDialog" header="Login" widgetVar="loginWidget" modal="true" visible="#{!accessBacking.hasAccess}" closable="false"&gt;
          &lt;h:form id="loginForm"&gt;
              &lt;p:messages id="loginFormMessages" severity="error" autoUpdate="true" showDetail="true" /&gt;
              &lt;h:panelGrid columns="2" cellspacing="10" width="300"&gt;  
  
              &lt;p:outputLabel for="username" value="Username" /&gt;  
              &lt;p:inputText id="username" value="#{accessBacking.username}" required="true" requiredMessage="Username is Required" /&gt;
  
              &lt;p:outputLabel for="password" value="Password" /&gt;  
              &lt;p:password id="password" value="#{accessBacking.password}" required="true" requiredMessage="Password is Required" /&gt;
  
              &lt;h:panelGroup&gt;&lt;/h:panelGroup&gt;
                  &lt;h:panelGroup&gt;
                      &lt;p:commandButton value="Login" styleClass="ui-priority-primary" actionListener="#{accessBacking.checkViewAccess}" oncomplete="handleAuthenticationRequest(xhr, status, args)" update="loginFormMessages" /&gt;
                  &lt;/h:panelGroup&gt;
              &lt;/h:panelGrid&gt; 
          &lt;/h:form&gt;
      &lt;/p:dialog&gt;
  ...
  </code></pre>
  
  <p>userGroups.xhtml</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version='1.0' encoding='UTF-8' ?&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"  
        xmlns:h="http://java.sun.com/jsf/html" 
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:ui="http://java.sun.com/jsf/facelets" 
        xmlns:p="http://primefaces.org/ui"&gt;
  
  &lt;h:body&gt;
  
      &lt;f:metadata&gt;
          &lt;f:event type="preRenderView" listener="#{userGroupBacking.setCurrentMenu}" /&gt;
      &lt;/f:metadata&gt;
  
      &lt;ui:composition template="/templates/template.xhtml"&gt;
          &lt;p:dataTable var="user" value="#{userGroupBacking.users}" editable="true" id="userTable"&gt;
  ...
  </code></pre>
  
  <p>AccessBacking.java</p>
  
  <pre><code>@ManagedBean(name="accessBacking")
  @SessionScoped
  public class AccessBacking {
          private String username;
      private String password;
  
      public boolean checkViewAccess() {
          Access access = new Access();
          if(access.authenticate(username, password)) {
            // user is logged in  
          }
      }
  }
  </code></pre>
  
  <p>UserGroupBacking</p>
  
  <pre><code>@ManagedBean(name="userGroupBacking")
  @ViewScoped
  public class UserGroupBacking {
  
          @ManagedProperty(value="#{accessBacking}")
      private AccessBacking accessBacking;
      public void setAccessBacking(AccessBacking accessBacking) {
          this.accessBacking = accessBacking;
      }
  
          @PostConstruct
      public void init() {
          loadData();
      }
  
          /**
       * Loads the data for the page
       */
      public void loadData() {
          Dao dao = new Dao(ds);
          users = dao.findAllUsers(accessBacking.getUsername(), accessBacking.getRoles());  // NULL POINTER BECAUSE ACCESSBACKING.GETUSERNAME() IS NULL SINCE THE USER HASN'T LOGGED IN YET.
      }
  }
  </code></pre>