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?
Use
StateHelper. It’s available byUIComponent#getStateHelper().Note that I’m returning a default value of
0in the getter. You might want to changeinttoIntegerand remove the default value so thatnullwill be returned.Unrelated to the concrete problem, you can for more simplicity also just extend
UINamingContainerinstead of implementingNamingContainer. This way you can omit the overriddengetFamily()method as it’s already implemented rightly byUINamingContainer.