How to save state when extending UIComponentBase
Asked Answered
B

1

6

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?

Blighter answered 23/2, 2012 at 0:11 Comment(0)
T
11

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.

Tole answered 23/2, 2012 at 3:24 Comment(7)
A quick followup, if I wanted to run some "One Time" initialization code, and save it to the StateHelper, what would be the appropriate place to do that? I tried the constructor BFTableComponent but that did not have the properties available yet, and it's run multiple times.Blighter
Is it static data? Make it a static variable. If initialization is complex, you could do it in a static {}.Tole
Nope, not static. one of the attributes is that a user can select the initial page. I want to set that exactly once. Is there a place for one time initialization?Blighter
Sounds more like candidate for a backing bean property, not an UIComponent attribute. Use a managed bean instead for that attribute.Tole
Ok. But for future reference, no such construct is available? There is no way to run initialization code for a FacesComponent?Blighter
You can use the constructor for that, but you're apparently relying on an external model value, for that you should be using a managed bean instead.Tole
did you find an appropriate solution, exabrial? I have a similar requirement (encapsulated state in the component with an inital value provided by client). But I am in doubt, that such requirement fits into the concept at all.Poachy

© 2022 - 2024 — McMap. All rights reserved.