Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Managed-Bean Data"

(Direct Entries)
m (Property Values)
Line 62: Line 62:
  
  
== Property Values ==
+
== 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:
 +
 
 +
 
 +
<pre>
 +
  ...
 +
  public String getMyProperty1()
 +
  {
 +
      return _myProperty1;
 +
  }
 +
 
 +
  public void setMyProperty1(String newValue)
 +
  {
 +
      _myProperty1 = newValue;
 +
  }
 +
  ...
 +
</pre>
 +
 
 +
 
 +
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:
 +
 
 +
<pre>
 +
    #{bean.myProperty1}
 +
</pre>
 +
 
 +
In this case, the application configuration file can also provide initialization for this bean using managed properties like this:
 +
 
 +
<pre>
 +
  <managed-bean>
 +
      <managed-bean-name>bean</managed-bean-name>
 +
      ...
 +
      <managed-property>
 +
        <property-name>myProperty1</property-name>
 +
        <value>foobar</value>
 +
      </managed-property>
 +
      ...
 +
    </managed-bean>
 +
</pre>
 +
 
 +
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:
 +
 
 +
<pre>
 +
  <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>
 +
</pre>

Revision as of 13:49, 4 October 2006

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>

Back to the top