我正在使用 JSF 2.0 Mojarra。
我需要创建一个包含 List 类型属性的托管 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 -->
在此先感谢您的帮助。