Java JSF 中的视图状态是什么,它是如何使用的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2910741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is viewstate in JSF, and how is it used?
提问by Matthew Farwell
In JSF, there is a viewstate associated with each page, which is passed back and forth with submits etc.
在 JSF 中,每个页面都有一个视图状态,它通过提交等来回传递。
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.
我知道 viewstate 是使用页面上各种控件的状态计算的,并且您可以将其存储在客户端或服务器端。
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.
此外,它是如何计算的 - 我意识到 Richfaces 的计算方式可能与 myfaces 不同,但一个想法会很好。
Thanks.
谢谢。
采纳答案by ewernli
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.
viewstate 存在的最初原因是因为 HTTP 是无状态的。跨请求的组件状态需要以一种或另一种方式维护。您可以将状态存储在服务器的内存中并将其绑定到会话,或者每次都在请求/响应中对其进行序列化/反序列化。
AFAIK, the viewstate is not used to detect double submit, but it could if you attach a timestamp or something similar to it.
AFAIK,viewstate 不用于检测双重提交,但如果您附加时间戳或类似的东西,它可以。
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.
此外,它是如何计算的 - 我意识到 Richfaces 的计算方式可能与 myfaces 不同,但一个想法会很好。
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.
每个组件都负责使用saveState
和来保持其状态restoreState
(请参阅本教程)。所以不同的组件套件会导致不同的视图状态。同样,不同的 JSF 实现可能会导致不同的视图状态。
回答by McDowell
If you're familiar with JavaScript, you can think of a JSF component tree a bit like a HTML DOM where the HTML page defines the initial state but you can alter it at runtime.
如果您熟悉 JavaScript,您可以将 JSF 组件树想成有点像 HTML DOM,其中 HTML 页面定义初始状态,但您可以在运行时更改它。
The view technology (usually JSP or Facelets) defines the initial state, but after that it can be manipulated programatically. For example, you could add a componentor set a property. In order for this to work properly, the view state must be persisted between requests.
视图技术(通常是 JSP 或 Facelets)定义了初始状态,但之后可以通过编程方式对其进行操作。例如,您可以添加一个组件或设置一个属性。为了使其正常工作,视图状态必须在请求之间保持不变。
The view state is divided into two parts. The first defines the structure of the component tree:
视图状态分为两部分。第一个定义了组件树的结构:
UIView
- UIForm
- UICommand
- UIInput
The second part defines the state of the components. These are separate due to components like UIData, where it is possible for children to have (for example) per row state. This is marshalled/unmarshalled via the StateHoldermechanisms.
第二部分定义组件的状态。由于 UIData 之类的组件,这些组件是分开的,其中子级可能具有(例如)每行状态。这是通过StateHolder机制编组/解组的。