Beans 上的 List、Map


如果您的Bean上有接受List或Map型態的屬性,則您也可以在組態檔案中直接設定這些屬性的值,一個例子如下:
 ....
 <managed-bean>
    <managed-bean-name>someBean</managed-bean-name>
    <managed-bean-class>
       onlyfun.caterpillar.SomeBean
    </managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>

    <managed-property>
        <property-name>someProperty</property-name>
        <list-entries>
            <value-class>java.lang.Integer</value-class>
            <value>1</value>
            <value>2</value>
            <value>3</value>
        </list-entries>
    </managed-property>
 </managed-bean>
 ....
 

這是一個設定接受List型態的屬性,我們使用<list-entries>標籤指定將設定一個List物件,其中<value- class>指定將存入List的型態,而<value>指定其值,如果是基本型態,則會嘗試使用指定的 <value-class>來作Wrapper類別。

設定Map的話,則是使用<map-entries>標籤,例如:
 ....
 <managed-bean>
    <managed-bean-name>someBean</managed-bean-name>
    <managed-bean-class>
       onlyfun.caterpillar.SomeBean
    </managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>

    <managed-property>
        <property-name>someProperty</property-name>
        <map-entries>
            <value-class>java.lang.Integer</value-class>
            <map-entry>
                <key>someKey1</key>
                <value>100</value>
            </map-entry>
            <map-entry>
                <key>someKey2</key>
                <value>200</value>
            </map-entry>
        </map-entries>
    </managed-property>
 </managed-bean>
 ....
 

由於Map物件是以key-value對的方式來存入,所以我們在每一個<map-entry>中使用<key>與< value>標籤來分別指定。

您也可以直接像設定Bean一樣,設定一個List或Map物件,例如在JSF附的範例中,有這樣的設定:
 ....
   <managed-bean>
    <description>
      Special expense item types
    </description>
    <managed-bean-name>specialTypes</managed-bean-name>
    <managed-bean-class>
        java.util.TreeMap
    </managed-bean-class>
    <managed-bean-scope>application</managed-bean-scope>
    <map-entries>
      <value-class>java.lang.Integer</value-class>
      <map-entry>
        <key>Presentation Material</key>
        <value>100</value>
      </map-entry>
      <map-entry>
        <key>Software</key>
        <value>101</value>
      </map-entry>
      <map-entry>
        <key>Balloons</key>
        <value>102</value>
      </map-entry>
    </map-entries>
  </managed-bean>
 ....
 

而範例中另一個設定List的例子如下:
 ....
  <managed-bean>
    <managed-bean-name>statusStrings</managed-bean-name>
    <managed-bean-class>
        java.util.ArrayList
    </managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
    <list-entries>
      <null-value/>
      <value>Open</value>
      <value>Submitted</value>
      <value>Accepted</value>
      <value>Rejected</value>
    </list-entries>
  </managed-bean>
 ....