Meaning of saveState() and restoreState()?

Hello,

I've done some tutorials on building custom JSF components. On some tutorials saveState() and restoreState are used, on others they are not used. I checked the sun j2ee 5 tutorial on jsf and it is stated that these functions have to used on components which implement StateHolder.

I removed the two functions from the tutorial mentioned above to test whether it works without and it did! The component attributes still remained after a submit.

So what's the meaning of these two functions? When do I have to override them? Because alle my sample components work with and without these functions the same way.

It is also interesting that these overriden functions don't get called from the framework (I used a debugger to validate this).

I really would appreciate any hints or help because I have benn trying to figure this out on myself for the last 6 hours without any luck.

Thanks in advance.

Walter

[960 byte] By [TheSulibana] at [2007-10-3 2:35:06]
# 1
The state saving means that the array with the state objects will be saved in the view tree for later restore.
amitteva at 2007-7-14 19:34:03 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Firstly, thanks for your answer.

But if I don't override these functions the state objects are also stored, so this seems obsolete to me. Or I am understanding something wrong here?

An example:

In my component class (derived from UICommand) I have the following attribute/getter/setter:

private int val = 1;

public void setVal(int val) { this.val= val; }

public int getVal() { return this.val; }

In my renderer class I have the following code (extract) in the decode-function:

UISpin comp = (UISpin)component;

if (requestMap.get(clientId + ".btnUpper") != null)

comp.setVal(comp.getVal()*2);

where comp is the reference to my UISpin-Component. That means if the button with the id clientID.btnUpper has caused the submit, the value of val should be recalculated by multiplicating with a factor of 2.

If I debug and check for the value returned by getVal() I initially get 1 (because that's the initial value of val) and then 2, 4, 8 and so on. I don't use any saveState or restoreState and nevertheless the correct value of val is saved in the view. So why does this work? If the saveState and restoreState functions were necessary, it should not save the value without it.

TheSulibana at 2007-7-14 19:34:03 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Has nobody an hint what I am understanding wrong here?Any help would be greatly appreciated.Thanks.Walter
TheSulibana at 2007-7-14 19:34:03 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

After some research I found the reason that these functions get not called in JBoss forum.

Quote:

For the second one about saveState/restoreState, you can never rely on them being called or not called (JSF spec 3.2.3.2). You are right in that MyFaces was forcing you to be compliant.

TheSulibana at 2007-7-14 19:34:03 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...