Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a list to a JSF component without using a bean

Tags:

jsf

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?

like image 551
Paul Wasilewski Avatar asked May 19 '12 18:05

Paul Wasilewski


1 Answers

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')}

like image 144
McDowell Avatar answered Oct 02 '22 14:10

McDowell