Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

java - How to save state when extending UIComponentBase

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...