Paul Wasilewski
Paul Wasilewski

Reputation: 10372

Pass a list to a JSF component without using a bean

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?

Upvotes: 6

Views: 2679

Answers (3)

Vsevolod Golovanov
Vsevolod Golovanov

Reputation: 4206

Collection construction is possible with EL 3.0.

Set:

{1, 2, 3}

List:

[1, "two", [foo, bar]]

Map:

{"one":1, "two":2, "three":3}

Example of using EL collection construction with JSF:

<h:dataTable value="#{[1, 2, 3]}" var="v">
    <h:column>#{v}</h:column>
</h:dataTable>

See the EL 3.0 Specification - final release, 2.2 Construction of Collection Objects.

Upvotes: 4

McDowell
McDowell

Reputation: 108859

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

Upvotes: 5

fdreger
fdreger

Reputation: 12495

If your question means something like "is there a list literal in EL?", then the answer is "there is none".

If you want to create a list and not use JSF backing beans, then the answer is: "use CDI beans or Spring beans".

If you simply don't like having model classes in your Java projects, then you can easily create and register an EL resolver that will create a new list when a specific name is used.

Either way, you are probably making some huge mistake or misunderstand something important. Maybe you should have asked a different question.

Upvotes: 2

Related Questions