|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface Stateful
Provides an interface that controls can implement that need to preserve state across multiple requests.
Please note: Control state is not saved and restored automatically by Click. Instead, state saving and restoring is under full control of the developer through a public API. Controls implementing this interface are expected tosave
and restore
their internal state, as well
as the state of their child controls. Controls can further expose a public
API for saving and restoring their state.
An example implementation is shown below:
public class MyControl extends AbstractControl implements Stateful { private String value; ... // Return the Control internal state public Object getState() { return getValue(); } // Set the Control internal state public void setState(Object state) { String fieldState = (String) state; setValue(fieldState); } // A save state helper method public void saveState(Context context) { // Save the control state in the session ClickUtils.saveState(this, getName(), context); } // A restore state helper method public void restoreState(Context context) { // Load the control state from the session ClickUtils.restoreState(this, getName(), context); } // A remove state helper method public void removeState(Context context) { // Remove the control state from the session ClickUtils.removeState(this, getName(), context); } }Saving and restoring Control state is controlled by the developer. For example:
public class MyPage extends Page { private MyControl control = new MyControl("mycontrol"); public MyPage() { // Load the control state from the session control.loadState(getContext()); } public void onPost() { Context context = getContext(); String id = context.getParameter("id"); control.setValue(id); // Save control state to the session control.saveState(context); } }
Method Summary | |
---|---|
Object |
getState()
Return the Control internal state. |
void |
setState(Object state)
Restore the Control internal state from the given state object. |
Method Detail |
---|
Object getState()
serializable
.
Example implementation below:
public Object getState() { Object stateArray[] = new Object[3]; stateArray[0] = getValue(); stateArray[1] = Number.valueOf(getNumber()); stateArray[2] = Boolean.valueOf(getBoolean()); return stateArray; }
void setState(Object state)
public void setState(Object state) { Object[] stateArray = (Object[]) state; String storedValue = stateArray[0]; setValue(storedValue); int storedNumber = ((Integer) stateArray[1]).intValue(); setNumber(storedNumber); boolean storedBoolen = ((Boolean) stateArray[2]).booleanValue(); setBoolean(storedBoolean); }
state
- the control state to restore
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |