通过faces-config.xml确定SelectItem数组值

时间:2013-12-17 10:24:16

标签: java xml jsf faces-config

在JSF页面中,我在<f:selectItems/>标记内有一个<h:selectOneMenu/>标记,我知道我可以从托管bean中确定项目:

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


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

但我想知道我是否可以通过XML确定这些值,如下所示:

<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 个答案:

答案 0 :(得分:1)

您可能需要以这种方式撰写bean:

<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>

注意,这是未经测试的,但您应该明白这一点。

答案 1 :(得分:1)

你需要做这样的事情......

<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>