3 Replies Latest reply on Dec 8, 2015 4:05 AM by Miha Vitorovič

    Getting the same managed bean in the same page

    Miha Vitorovič Newbie

      Imagine I have two managed beans, both of them are @ViewScoped

       

      package test.app;
      
      import java.io.Serializable;
      import javax.faces.bean.ManagedBean;
      import javax.faces.bean.ViewScoped;
      
      @ManagedBean(name = "randomData")
      @ViewScoped
      public class RandomData implements Serializable {
          private String randomUniqueData = String.format("%X", System.nanoTime());
      
          public RandomData() {}
      
          public String getRandomUniqueData() {
              return randomUniqueData;
          }
      }
      

       

      and

       

      package test.app;
      
      import java.io.Serializable;
      import java.util.ArrayList;
      import java.util.List;
      
      import javax.faces.bean.ManagedBean;
      import javax.faces.bean.ViewScoped;
      import javax.inject.Inject;
      
      @ManagedBean(name = "personUi")
      @ViewScoped
      public class PersonUi implements Serializable {
          @Inject private RandomData randomData;
      
        public String getInjectedRandomData() {
           return randomData.getRandomUniqueData();
        }
      }
      

       

      And a web page:

       

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <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">
      
      <h:head>
      </h:head>
      <h:body>
        <h1>Hello World !</h1>
        <h:form>
      
              <h:outputText value="#{personUi.injectedRandomData}" /> ?= <h:outputText value="#{randomData.randomUniqueData}" />
      
      
        </h:form>
      
      </h:body>
      </html>
      

       

      How can I make the two managed beans one and the same? Explicitly, why is the managed bean accessed from the page directly a different one that a managed bean injected into PersonUI.

       

      I am doing this on Wildfly 8.2.

       

      Thanks in advance and br, Miha