JSF 中的视图状态是什么?如何使用它?
在 JSF 中,每个页面都有一个关联的视图状态,它通过提交等来回传递。
我知道视图状态是使用页面上各种控件的状态计算的,并且您可以将其存储在客户端或服务器端边。
问题是:这个值如何使用?它是否用于验证提交时发送的值,以确保同一请求不会发送两次?
另外,它是如何计算的 - 我意识到 richfaces 的计算方式可能与 myfaces 不同,但一个想法会很好。
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
viewstate存在的最初原因是因为HTTP是无状态的。需要以一种或另一种方式维护跨请求的组件的状态。要么将状态存储在服务器的内存中并将其绑定到会话,要么每次在请求/响应中对其进行序列化/反序列化。
AFAIK,视图状态不用于检测双重提交,但如果您附加时间戳或类似的内容,则可以。
视图状态也可以被加密以确保客户端不会更改它。
每个组件负责通过
saveState
和restoreState
保存其状态(请参阅此 教程)。因此不同的组件套件会导致不同的视图状态。类似地,不同的 JSF 实现可能会导致不同的视图状态。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.
Each component is responsible to persist its state with
saveState
andrestoreState
(see this tutorial). So different component suites result in different view state. Similarly, different JSF implementations might result in different view state.如果您熟悉 JavaScript,您可以将 JSF 组件树想象成有点像 HTML DOM,其中 HTML 页面定义初始状态,但您可以在运行时更改它。
视图技术(通常是 JSP 或 Facelets)定义初始状态,但之后可以通过编程方式对其进行操作。例如,您可以添加 组件< /a> 或设置 属性。为了使其正常工作,视图状态必须在请求之间保持不变。
视图状态分为两部分。第一部分定义组件树的结构:
第二部分定义组件的状态。由于像 UIData 这样的组件,它们是分开的,其中子级可能具有(例如)每行状态。这是通过 StateHolder 机制。
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.
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 component or set a property. In order for this to work properly, the view state must be persisted between requests.
The view state is divided into two parts. The first defines the structure of the component tree:
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 StateHolder mechanisms.