0
votes

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?

1
So, it's in your webapp possible that the logged-in user get changed in between the initial request of a view and a successful postback to the very same view?BalusC
Correct. It's possible that a first user logs in, then logs out, then another user logs in using the same web page.perissf

1 Answers

1
votes

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.