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.
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
.