2

我正在使用JSF 2.0 Mojarra。JSF 2.0 ArrayList属性Managed Bean

我需要创建一个托管Bean,其中包含List类型的属性。我需要用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 --> 

在此先感谢您的帮助。

回答

0

你引用列表项目作为普通的香草字符串。

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

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

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

嘿@BalusC,非常感谢。我认为Faces-Config是正确的。这个类的结构如何?我如何正确设置它,以便JSF可以初始化所有内容? – bricks