Managed Bean的JSF 2.0 ArrayList属性

时间:2012-06-29 00:23:05

标签: jsf inversion-of-control javabeans managed-bean faces-config

我正在使用JSF 2.0 Mojarra。

我需要创建一个包含List类型属性的Managed Bean。我需要在Faces-Config.xml中使用一些值初始化此列表。我的问题是,我的班级结构需要看起来是什么样的。

例如,

public class Items{
     private List<Item> itemList = new ArrayList<>();

     public List<Item> getItemList(){
     return itemList;
}

public void setItemList (List<Item> itemList){
     this.itemList = itemList;
}

//Methods needed for adding and removing type Item elements to/from itemList.
//What is the convention, so that JSF can initialize these values?

public class Item{
     //This is a nested class
     private String itemProperty1;
     private String itemProperty2;

     //Getters and Setters for itemProperty1 and itemProperty2 have been omitted
     //for brevity.
   }
}

此外,我的班级设置正确。 Faces-Config.xml的正确结构是什么。例如,我应该这样做:

 <managed-bean>
   <managed-bean-name>items</managed-bean-name>
   <managed-bean-class>com.bricks.model.Items</managed-bean-class>
   <managed-bean-scope>Application</managed-bean-scope>
   <managed-property>
     <property-name>itemList</property-name>
     <value-class>com.brick.model.Items.Item</value-class>
     <list-entries>
       <value>item1</value>
       <value>item2</value>
     </list-entries>
  </managed-property>
</managed-bean>

<managed-bean>
  <managed-bean-name>item1</managed-bean-name>
  <managed-bean-class>com.bricks.model.Item</managed-bean-class>
  <managed-bean-scope>None</managed-bean-scope>
  <managed-property>
    <property-name>itemProperty1</property-name>
    <value>value1</value>
  </managed-property>
  <managed-property>
    <property-name>itemProperty2</property-name>
    <value>value2</value>
  </managed-property>
</managed-bean>

 <!--Repeat for item2 -->

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您将列表项引用为普通字母字符串。

<value>item1</value>
<value>item2</value>

您需要通过EL引用它们,以便它解析为托管Item实例。

<value>#{item1}</value>
<value>#{item2}</value>