HTML and CSS Reference
In-Depth Information
public void setCount(Integer count) {
getStateHelper().put(ATTR_COUNT, count);
}
public Integer getMinWords() {
return (Integer) getStateHelper().eval(ATTR_MIN_WORDS, ATTR_MIN_WORDS_DEFAULT);
}
public void setMinWords(Integer minWords) {
getStateHelper().put(ATTR_MIN_WORDS, minWords);
}
public Integer getMaxWords() {
return (Integer) getStateHelper().eval(ATTR_MAX_WORDS, ATTR_MAX_WORDS_DEFAULT);
}
public void setMaxWords(Integer maxWords) {
getStateHelper().put(ATTR_MAX_WORDS, maxWords);
}
}
You have probably noticed that the getters and setters in Listing 6-3 are not your typical getters and setters
encapsulating a class member. Instead they use the StateHelper exposed on the UIComponent class. If the attributes
simply used class members to store their values, the values would disappear after every request, as they are not persisted
anywhere. All UIComponents implement the PartialStateHolder interface with the intent that each UIComponent must
manage its own state. All standard components implement the PartialStateHolder and use the StateHelper to persist
and retrieve the necessary data. However, if you extend UIComponent rather than a standard component you must
manage the state of the component yourself. Considering that the JSF implementation may store the component state on
either the client or server side (depending on the value of the javax.faces.STATE_SAVING_METHOD context parameter)
it could potentially require a lot of work for a component writer to implement state management. Luckily, the authors
of JSF realized that and provide the StateHelper class to any class that implements UIComponent . The StateHelper
transparently takes care of saving and restoring the state of a component between views. See the StateHolder class
hierarchy and methods in Figure 6-3 . Basically, the StateHelper allows us to put an object into a map with a serializable
name. Later we can fetch (evaluate) the object using the same serializable name. If a requested name is not available
a null object is returned. To avoid checking for null values, the StateHelper has an overloaded eval method where
you specify the name of the object you are looking for and the value that should be returned in case it does not find the
requested object. This is handy for providing default values for attributes.
Search WWH ::




Custom Search