2
votes

I am using JSF 2.0 Mojarra.

I need to create a Managed Bean that contains a property of type List. I need to initialize this list with some values in the Faces-Config.xml. My question is, what does the structure of my Class need to look like to accomplish this.

For example,

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

Further, once I have my Class set up properly. What is the correct structure of the Faces-Config.xml. For example, should I do it like this:

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

Thanks in advance for the help.

1

1 Answers

0
votes

You're referencing list items as plain vanilla strings.

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

You need to reference them by EL so that it resolves to the managed Item instances.

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