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")
      public class RandomData implements Serializable {
          private String randomUniqueData = String.format("%X", System.nanoTime());
          public RandomData() {}
          public String getRandomUniqueData() {
              return randomUniqueData;




      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")
      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" 
      <html xmlns="http://www.w3.org/1999/xhtml"
        <h1>Hello World !</h1>
              <h:outputText value="#{personUi.injectedRandomData}" /> ?= <h:outputText value="#{randomData.randomUniqueData}" />


      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