0

I want to show a page that renders a list of 'Group' entities, so my page (xhtml) has this in it

<f:event type="preRenderView" listener="#{groupController.loadGroups}" />

This is called when the page loads, and yesterday it was returning a list as expected. The method looks like this

public void loadGroups() {
    allGroups = groupDao.findAll();
}

Now I added a form to save a new group (on the same page). When I save that form, it calls this method:

public String save() {
    groupDao.create(group);
    return "/admin/groups?faces-redirect=true";
}

I have confirmed that the group is saved in the database. But, I was expecting the "return" statement to reload the page as if entering it for the first time, calling the 'preRenderView' method etc etc.

When it comes back into the page it does call that method, but am using a datalist to display the collection of groups returned and when that makes a call to

public Collection<Group> getAllGroups() {
    return allGroups;
}

... the 'allGroups' variable has been reset back to null.

I haven't touched JSF for a couple of months and I think I used to understand all this but I obviously don't. Could anyone give me a clue what I'm doing wrong?

If I find the answer (digging my books out right now) I will post an update. Thanks though.

Update: just to clarify, I thought the loadGroups method would be executed before the getAllGroups method, since it's a preRenderView event. But they seem to be getting executed the wrong way around (getAllGroups is called first, before loadGroups() has populated the collection). It's a ViewScoped bean by the way.

Richard
  • 1,731
  • 2
  • 23
  • 54

1 Answers1

0

I'm not sure why my original version doesn't work, but if I get rid of the preRenderView statement in my page, and add a @PostConstruct annotation on the loadGroups method, it works fine. I understand why the @PostConstruct method would work, but not why the preRenderView doesn't work. Anyway, its working if I do that...

Richard
  • 1,731
  • 2
  • 23
  • 54