perissf
perissf

Reputation: 16273

How to pass a parameter to JSF dataTable's value attribute

In a JSF 2.1 application, I need to build a JSF dataTable (using PrimeFaces) that shows only the db records belonging to the logged in user.

So, I need to pass the username to the bean associated to the dataTable's value attribute:

value="#{tableBuilder.records}"

Here is the table bean:

@ManagedBean
@ViewScoped
public class TableBuilder {
    private List<MyRecord> records;
    private String username;
    // getters and setters
}

It's useful to know that the application consists of a single web page, with container-managed authentication implemented through LoginBean, a SessionScoped ManagedBean. This implies the additional effort of notifying to TableBuilder when the user logs in.

The only way I am thinking of is to inject the LoginBean into the TableBuilder through @ManagedProperty annotation, and checking on every request of getRecords if the username property of LoginBean has changed.

Maybe there are better ways?

Upvotes: 0

Views: 2968

Answers (1)

BalusC
BalusC

Reputation: 1108632

If your environment supports EL 2.2 (your question history confirms Java EE 6), then "just do it":

<h:dataTable value="#{bean.getModel(user)}">

with

public List<Item> getModel(User user) {
    // ...
}

Whether it's the right way or there are better ways, I'll leave in the middle. Keep in mind that a getter is invoked as many times as EL evaluates the value expression.

Upvotes: 1

Related Questions