Determine SelectItem Array Values via faces-config.xml

287 views Asked by At

In a JSF page I have a <f:selectItems/> tag inside a <h:selectOneMenu/> tag, and I know that I can determine the items from the managed bean:

<f:selectItems value="#{bean.items}" />


SelectItem[] items = {new SelectItem("1","value 1"),new SelectItem("2","value 2")};

But I was wondering if I could determine these values via XML, something like this:

<managed-bean>
        <managed-bean-name>bean</managed-bean-name>
        <managed-bean-class>chat.LoginBean</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
        <managed-property>
            <property-name>items</property-name>
            <list-entries>
                <value-class>javax.faces.model.SelectItem</value-class>
                <value>value 1</value> <!-- The issue is here, there's a value and a label...what should I do? -->
            </list-entries>
        </managed-property>
</managed-bean>
2

There are 2 answers

1
Apurv On BEST ANSWER

You need to do something like this ...

<managed-bean>
    <managed-bean-name>myBean</managed-bean-name>
    <managed-bean-class>
        com.MyBean
    </managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
    <managed-property>
        <property-name>list</property-name>
        <list-entries>
            <value-class>javax.faces.model.SelectItem</value-class>
            <value>#{selectItem1}</value>
            <value>#{selectItem2}</value>
            <value>#{selectItem3}</value>
        </list-entries>
    </managed-property>

</managed-bean>
<managed-bean>
    <managed-bean-name>selectItem1</managed-bean-name>
    <managed-bean-class>javax.faces.model.SelectItem</managed-bean-class>
    <managed-bean-scope>none</managed-bean-scope>
    <managed-property>
        <property-name>label</property-name>
        <property-class>java.lang.String</property-class>
        <value>Value1</value>
    </managed-property>
    <managed-property>
        <property-name>value</property-name>
        <property-class>java.lang.Object</property-class>
        <value>1</value>
    </managed-property>
</managed-bean>
<managed-bean>
    <managed-bean-name>selectItem2</managed-bean-name>
    <managed-bean-class>javax.faces.model.SelectItem</managed-bean-class>
    <managed-bean-scope>none</managed-bean-scope>
    <managed-property>
        <property-name>label</property-name>
        <property-class>java.lang.String</property-class>
        <value>Value2</value>
    </managed-property>
    <managed-property>
        <property-name>value</property-name>
        <property-class>java.lang.Object</property-class>
        <value>2</value>
    </managed-property>
</managed-bean>
<managed-bean>
    <managed-bean-name>selectItem3</managed-bean-name>
    <managed-bean-class>javax.faces.model.SelectItem</managed-bean-class>
    <managed-bean-scope>none</managed-bean-scope>
    <managed-property>
        <property-name>label</property-name>
        <property-class>java.lang.String</property-class>
        <value>Value3</value>
    </managed-property>
    <managed-property>
        <property-name>value</property-name>
        <property-class>java.lang.Object</property-class>
        <value>3</value>
    </managed-property>
</managed-bean>
0
McDowell On

You will probably need to compose the beans in this manner:

<managed-bean>
  <managed-bean-class>javax.faces.model.SelectItem</managed-bean-class>
  <managed-bean-name>item1</managed-bean-name>
  <managed-bean-scope>request</managed-bean-scope>
  <managed-property>
    <property-name>label</property-name>
    <value>Foo</value>
  </managed-property>
  <managed-property>
    <property-name>value</property-name>
    <value>foo</value>
  </managed-property>
</managed-bean>
<managed-bean>
  <managed-bean-class>foo.MyBean</managed-bean-class>
  <managed-bean-name>item1</managed-bean-name>
  <managed-bean-scope>request</managed-bean-scope>
  <managed-property>
    <property-name>items</property-name>
    <list-entries>
      <value>#{item1}</value>
    </list-entries>
  </managed-property>
</managed-bean>

Note, this is untested, but you should get the idea.