How to save state when extending UIComponentBase

后端 未结 1 1068
不知归路
不知归路 2020-11-28 14:07

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.

相关标签:
1条回答
  • 2020-11-28 14:40

    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.

    0 讨论(0)
提交回复
热议问题