3 Replies Latest reply on Dec 8, 2015 4:05 AM by mvitorovic

    Getting the same managed bean in the same page

    mvitorovic

      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

        • 1. Re: Getting the same managed bean in the same page
          ctomc

          my guess would be as you are mixing jsf and cdi injection and each one has its own lifecycle.

           

          try using @javax.inject.Named("personUi") instead of @ManagedBean(name = "personUi") 

          and same for randomData class.

          • 2. Re: Getting the same managed bean in the same page
            mvitorovic

            Thanks for your answer, but that didn't work. But during testing this approach a searched for a solution again, and this time I must have hit the correct search term, because I found the solution:

             

            Injecting Managed beans in JSF 2.0

             

            Works like a charm!

             

            Thanks again, your answer put me on the right track.

             

            Miha

            • 3. Re: Getting the same managed bean in the same page
              mvitorovic

              Tomaz Cerar wrote:

               

              my guess would be as you are mixing jsf and cdi injection and each one has its own lifecycle.

               

              try using @javax.inject.Named("personUi") instead of @ManagedBean(name = "personUi")

              and same for randomData class.

              Also, I did managed to make it work as you suggested as well (previously I had my pom.xml misconfigured). After switching javaee-api to 7.0 this works as well:

               

              import javax.faces.view.ViewScoped;
              import javax.inject.Named;
              
              @Named("randomData")
              @ViewScoped
              public class RandomData implements Serializable {
              

               

              and

               

              import javax.faces.view.ViewScoped;
              import javax.inject.Inject;
              import javax.inject.Named;
              
              @Named("personUi")
              @ViewScoped
              public class PersonUi implements Serializable {
                  @Inject private RandomData randomData;
              

               

              Thanks again, miha