Jump to: navigation, search

Managed-Bean Data

Background

Managed bean data allows a JSF application configuration file to set values on managed beans automatically when they are placed in scope. There are two types of managed-bean data: direct entries and managed properties.

Direct Entries

If a managed bean implements the Map or List interface, then managed-bean data can be configured directly on the bean itself. Consider the following snippet of application configuration XML for a managed bean whose class implements Map:

   <managed-bean>
      <managed-bean-name>myBean</managed-bean-name>
      ...
      <map-entries>
         <map-entry>
            <key>size</key>
            <value>10</value>
         </map-entry>

         <map-entry>
            <key>color</key>
            <value>blue</value>
         </map-entry>
      </map-entries>
      ...
   </managed-bean>

Instances of the bean can referenced in EL like this:

   #{myBean.size}

or this

   #{myBean['size']}

JSF EL tooling will add support for these types of properties. There is also an option to specify the type of either the key or the value or both:

   ...
   <map-entries>
      <key-class>java.lang.String</key-class>
      <value-class>java.lang.String</value-class>
      <map-entry>
          <key>color</key>
          <value>blue</key>
      </map-entry>
    ...

In this case we can further validate the type of an expression such as:

   #{bean.color}

as a String type.


Managed Properties

Managed properties are similar to direct values except that they apply to the properties of a managed bean, rather than the bean itself. Consider a managed bean, that does not implement Map or List, that has the following code:


   ...
   public String getMyProperty1()
   {
      return _myProperty1;
   }

   public void setMyProperty1(String newValue)
   {
      _myProperty1 = newValue;
   }
   ...


By the rules of JavaBeans, this bean has a property called myProperty1 that is both readable and writable. So we can use the following EL to express the property as either an r-value or l-value:

    #{bean.myProperty1}

In this case, the application configuration file can also provide initialization for this bean using managed properties like this:

   <managed-bean>
      <managed-bean-name>bean</managed-bean-name>
      ...
      <managed-property>
         <property-name>myProperty1</property-name>
         <value>foobar</value>
      </managed-property>
      ...
    </managed-bean>

In this situation, the expression above will resolve to the value "foobar" at runtime.


Using EL in Managed-Bean Data

In both direct and managed property values data, EL can be used to assign an instance of one bean or bean property to another. Consider the following snippet of application configuration XML:

   <managed-bean>
      <managed-bean-name>bean</managed-bean-name>
      <managed-bean-class>com.example.MyBean</managed-bean-class>
      ...
      <managed-property>
         <property-name>myProperty1</property-name>
         <value>foobar</value>
      </managed-property>
      ...
    </managed-bean>
    ...
   <managed-bean>
      <managed-bean-name>usesBean</managed-bean-name>
         ...
         <map-entries>
            <key-class>java.lang.String</key-class>
            <value-class>com.example.MyBean</value-class>
            <map-entry>
                <key>foo</key>
                <value>#{bean}</value>
            </map-entry>
         </map-entries>
         ...
      </managed-bean-name>
   </managed-bean>

Notice a few things. First, we have created two managed bean instances called bean and usesBean. In creating usesBean (which is assumed to be a Map), we have initialized a key/value on it called foo that points bean. In other words, the following EL:

   #{usesBean['foo']}

will return bean. Furthermore, because we have initialized bean's property myProperty1 with the value "foobar", we also know that at runtime the following EL:

   #{usesBean['foo'].myProperty1}

will resolve to the string value "foobar".


Consequences for EL tooling

As a consequence of managed-bean data EL tooling can support enhanced support in its managed bean symbol source provider that:

  • loads key and value data for managed beans that are Maps or Lists
  • loads value data for managed properties
  • can resolve EL expressions in values for managed-bean data