Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

java - What is viewstate in JSF, and how is it used?

In JSF, there is a viewstate associated with each page, which is passed back and forth with submits etc.

I know that viewstate is calculated using the states of the various controls on the page, and that you can store it either client side or server side.

The question is: how is this value used? Is it used to validate the values sent at submit, to ensure that the same request is not sent twice?

Also, how is it calculated - I realise that richfaces may be calculated differently from myfaces, but an idea would be nice.

Thanks.

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

The question is: how is this value used? Is it used to validate the values sent at submit, to ensure that the same request is not sent twice?

The original reason why the viewstate exists is because HTTP is stateless. The state of the components across requests need to be maintained one way or the other. Either you store the state in memory on the server and bind it to the session, or serialize/deserialize it in the request/response each time.

AFAIK, the viewstate is not used to detect double submit, but it could if you attach a timestamp or something similar to it.

The viewstate can also be encrypted to make sure the client doesn't alter it.

Also, how is it calculated - I realise that richfaces may be calculated differently from myfaces, but an idea would be nice.

Each component is responsible to persist its state with saveState and restoreState (see this tutorial). So different component suites result in different view state. Similarly, different JSF implementations might result in different view state.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...