how can I pass a List to a JSF-Component in EL without a backingbean? Or in other words how can I declare and initialize a List/Array in JSF without a bean?
Example:
caller.xhtml
/* Call a JSF-Component */
<mycomp:displayList value="HERE I WANT TO PASS A LIST WITHOUT A BACKINGBEAN"/>
displayList.xhtml
/* MY Component */
<composite:interface>
<composite:attribute name="value" type="java.util.list" />
</composite:interface>
Is there any possibility to pass a List/Collection which is not declared in a Bean to a JSF component?
Although there is no list literal in EL, you can declare a list without requiring a bean to contain it by declaring one in faces-config.xml:
<managed-bean>
<managed-bean-name>someList</managed-bean-name>
<managed-bean-class>java.util.ArrayList</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<list-entries>
<value>One</value>
<value>Two</value>
<value>Three</value>
</list-entries>
</managed-bean>
You can also use a utility type to build a list:
import java.util.*; import javax.faces.bean.*;
@ManagedBean @NoneScoped
public class Lister<T> extends AbstractList<T> {
private List<T> list = new ArrayList<T>();
public Lister plus(T t) {
list.add(t);
return this;
}
@Override public T get(int index) {
return list.get(index);
}
@Override public int size() {
return list.size();
}
@Override public String toString() {
return list.toString();
}
}
This can be used with an expression like #{lister.plus('one').plus('two').plus('three')}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With