Consider:
<h:body>
<h:dataTable value="#{integerListEntriesBean.racePlacement}" var="placement">
<h:column>
<h:outputText value="#{placement}" />
</h:column>
</h:dataTable>
</h:body>
The ManagedBean:
public class IntegerListEntriesBean {
// Notice carefully here just an object reference of type List<> exists
private List<Integer> racePlacement;
public List<Integer> getRacePlacement() {
return racePlacement;
}
public void setRacePlacement(List<Integer> racePlacement) {
this.racePlacement = racePlacement;
}
}
Entry in the faces-config.xml:
<managed-bean>
<managed-bean-name>integerListEntriesBean</managed-bean-name>
<managed-bean-class>com.jsf.ch5.IntegerListEntriesBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>racePlacement</property-name>
<property-class>java.util.List</property-class>
<list-entries>
<value-class>java.lang.Integer</value-class>
<value>30</value>
<value>10</value>
<value>50</value>
<value>40</value>
<value>20</value>
</list-entries>
</managed-property>
</managed-bean>
O/P:
30
10
50
40
20
So, ofcourse something this happened internally: List racePlacement = new ArrayList();
Now, if i make a slight change in the ManagedBean so as to add elements to the ArrayList inside the contructor
public class IntegerListEntriesBean {
// Notice carefully here I had to explicitly create an object here.
private List<Integer> racePlacement = new ArrayList<Integer>();
public IntegerListEntriesBean(){
racePlacement.add(500);
racePlacement.add(600);
}
public List<Integer> getRacePlacement() {
return racePlacement;
}
public void setRacePlacement(List<Integer> racePlacement) {
this.racePlacement = racePlacement;
}
}
O/P now:
500
600
30
10
50
40
20
Since the object (new ArrayList<>()) gets created internally(meaning manually not created by me), Why do I need to explicitly create the object in the ManagedBean now(in the second case)? i.e
List<Integer> racePlacement = new ArrayList<Integer>();
This is necessary because the constructor is executed before faces-config processor. racePlacement must be a object to support 'add' method.