How to save state when extending UIComponentBase

I am creating a composite component that will wrap a datatable in order to implement very simple paging. I need to keep state (current page number) between ajax requests.

I tried to create the fields in my FacesComponent, but found that they were destroyed during the JSF life cycle:

@FacesComponent(value = "bfTableComponent") public class BFTableComponent extends UIComponentBase implements NamingContainer { private int currentPageNumber; ... 

I cannot find a quick guide to do this anywhere! How to maintain state between requests when creating a composite component?

+4
source share
1 answer

Use StateHelper . It is accessible 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 am returning the default value of 0 to getter. You can change int to Integer and remove the default value to return null .


Unrelated to a specific problem, you can simply extend the UINamingContainer instead of implementing the NamingContainer for greater simplicity. That way, you can omit the overridden getFamily() method since it is already correctly implemented using the UINamingContainer .

+9
source

Source: https://habr.com/ru/post/1397921/


All Articles