3 Replies Latest reply on Apr 6, 2010 11:47 PM by Denis Golovin

    Initializing a Managed Bean's Property

    John Bailo Master

      I have a managed bean that calls a server via a URL.

       

      Since this will rarely change, I thought I'd add it to the faces-config.xml so that it would be a default value.

       

      I added the property to my bean via Eclipse and it created the get/set accessors (not sure if these are needed, since they were optional).

       

      I added the value to my managed bean's property.

       

      I see that a global String was added to the bean class with the name of the new property.

       

       

      However, when I call the bean, via a method I use to create a List and populate a dataTable in a JSF page, I find that the server URL variable is not initialized.

       

      What do I have to do to get the bean to read its default managed property?

        • 1. Re: Initializing a Managed Bean's Property
          Denis Golovin Master

          As it says here http://www.oracle.com/technology/tech/java/newsletter/articles/jsf_pojo/index.html, you need bean to be registered in faces config with default values. Then if you have a reference to it through EL expression somewhere on page it should just work.

           

          But it is rather JSF runtime related question then JSF tooling support. JSF Tools could be helpful here if it would be able outguess problem you faced and warn you that your bean might be not initialized properly at some point for some reason. I'm not sure it is possible since there are no source snippets in your question.

          1 of 1 people found this helpful
          • 2. Re: Initializing a Managed Bean's Property
            John Bailo Master

            I do have a default value set.

             

            The value is a connection to a server...that is used in a method when making a connection to the server.  (LDAP).

             

            I was hoping that at the time my JSF page called the bean, it would have read in the default values and be able to use them in executing the method called by JSF.   But that doesn't appear to be the case.

             

            I was looking for something like a properties file that is read on instantiation.  Again it seems like manged properties of the managed bean would do this -- but I am not sure at what point the bean is instantiated and what is required to get the default value read into the bean.  From what I read it seemed like it should just happen automatically once I established the managed property and added a default value.

            • 3. Re: Initializing a Managed Bean's Property
              Denis Golovin Master

              John Bailo wrote:

               

              I do have a default value set.

               

              I was hoping that at the time my JSF page called the bean, it would have read in the default values and be able to use them in executing the method called by JSF.   But that doesn't appear to be the case.

              It should depend on bean scope. For instance request scope bean will get re-initialized on each page refresh.

              John Bailo wrote:

               

              I was looking for something like a properties file that is read on instantiation.  Again it seems like manged properties of the managed bean would do this -- but I am not sure at what point the bean is instantiated and what is required to get the default value read into the bean.  From what I read it seemed like it should just happen automatically once I established the managed property and added a default value.

              I'm just guessing but why not this approach I get from http://www.icefaces.org

               

              "The simplest way is to use Chaining in the faces config and call your init method. You'll have to add an empty Object of String parameter to your a setInit method.

               

              ...
              <managed-property>
              <property-name>init</property-name>
              <value>java.lang.Object</value>
              </managed-property>
              ....
              

               

              From a design point of view I don't like my beans doing a whole lot of initialization work I usually make a naming distinction and move all initialization code into controller. The controller can initialized the bean directly by a callback.

               

              ...
              <managed-property>
              <property-name>init</property-name>
              <value>#{myController}</value>
              </managed-property>
              ....
              

               

              where

               

              pubic void setInit(MyController controller){
              controller.init(this);
              }
              

              "