As to the cause, UIComponent
instances are inherently request scoped. The postback effectively creates a brand new instance with properties like values
reinitialized to default. In your implementation, it is only filled during encodeXxx()
, which is invoked long after decode()
wherein the action event needs to be queued and thus too late.
You’d better fill it during the initialization of the component. If you want a @PostConstruct
-like hook for UIComponent
instances, then the postAddToView
event is a good candidate. This is invoked directly after the component instance is added to the component tree.
<cc:implementation>
<f:event type="postAddToView" listener="#{cc.init}" />
...
</cc:implementation>
with
private List<String> values;
public void init() {
values = new ArrayList<String>();
Integer num = (Integer) getAttributes().get("value");
for (int i = 0; i < num; i++) {
values.add("item" + i);
}
}
(and remove the encodeBegin()
method if it isn’t doing anything useful anymore)
An alternative would be lazy initialization in getValues()
method.