考虑:
<h:body>
<h:dataTable value="#{integerListEntriesBean.racePlacement}" var="placement">
<h:column>
<h:outputText value="#{placement}" />
</h:column>
</h:dataTable>
</h:body>
托管豆:
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;
}
}
在人脸配置.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>
操作/操作:
30
10
50
40
20
所以,当然,这在内部发生了一些事情:List racePlacement = new ArrayList();
现在,如果我在托管Bean中进行轻微更改,以便将元素添加到构造器内的ArrayList中
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:
500
600
30
10
50
40
20
由于对象(新的 ArrayList<>()))是在内部创建的(意味着不是由我手动创建的),为什么我现在需要在托管Bean中显式创建对象(在第二种情况下)?即
List<Integer> racePlacement = new ArrayList<Integer>();
这是必需的,因为构造函数是在 faces-config 处理器之前执行的。 racePlacement必须是支持"add"方法的对象。