6

I'm creating a composite component that will wrap a datatable to implement very simple paging. I need to save state (the current page number) between ajax requests.

I tried creating fields in my FacesComponent, but I discovered they are wiped out during the JSF lifecycle:

@FacesComponent(value = "bfTableComponent")
public class BFTableComponent extends UIComponentBase implements NamingContainer {

    private int currentPageNumber;
    ...

I can't seems to find a concise guide to doing this anywhere! How would one save state between requests when creating a composite component?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Jonathan S. Fisher
  • 8,189
  • 6
  • 46
  • 84

1 Answers1

11

Use StateHelper. It's available by UIComponent#getStateHelper().

private enum PropertyKeys {
    currentPageNumber;
}

public void setCurrentPageNumber(int currentPageNumber) {
    getStateHelper().put(PropertyKeys.currentPageNumber, currentPageNumber);
}

public int getCurrentPageNumber() {
    return (int) getStateHelper().eval(PropertyKeys.currentPageNumber, 0);
}

Note that I'm returning a default value of 0 in the getter. You might want to change int to Integer and remove the default value so that null will be returned.


Unrelated to the concrete problem, you can for more simplicity also just extend UINamingContainer instead of implementing NamingContainer. This way you can omit the overridden getFamily() method as it's already implemented rightly by UINamingContainer.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • A quick followup, if I wanted to run some "One Time" initialization code, and save it to the StateHelper, what would be the appropriate place to do that? I tried the constructor BFTableComponent but that did not have the properties available yet, and it's run multiple times. – Jonathan S. Fisher Feb 23 '12 at 05:06
  • Is it static data? Make it a `static` variable. If initialization is complex, you could do it in a `static {}`. – BalusC Feb 23 '12 at 11:56
  • Nope, not static. one of the attributes is that a user can select the initial page. I want to set that exactly once. Is there a place for one time initialization? – Jonathan S. Fisher Feb 23 '12 at 14:14
  • Sounds more like candidate for a backing bean property, not an UIComponent attribute. Use a managed bean instead for that attribute. – BalusC Feb 23 '12 at 14:17
  • Ok. But for future reference, no such construct is available? There is no way to run initialization code for a FacesComponent? – Jonathan S. Fisher Feb 23 '12 at 14:23
  • You can use the constructor for that, but you're apparently relying on an external model value, for that you should be using a managed bean instead. – BalusC Feb 23 '12 at 14:31
  • did you find an appropriate solution, exabrial? I have a similar requirement (encapsulated state in the component with an inital value provided by client). But I am in doubt, that such requirement fits into the concept at all. – Steve Oh Oct 27 '13 at 19:44