简体   繁体   English

什么是JSF中的viewstate,它是如何使用的?

[英]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. 在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. 另外,它是如何计算的 - 我意识到富脸可能与myfaces的计算方式不同,但一个想法会很好。

Thanks. 谢谢。

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. viewstate存在的原因是因为HTTP是无状态的。 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. AFAIK,视图状态不用于检测双重提交,但如果您附加时间戳或类似的东西,它可以。

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. 另外,它是如何计算的 - 我意识到富脸可能与myfaces的计算方式不同,但一个想法会很好。

Each component is responsible to persist its state with saveState and restoreState (see this tutorial ). 每个组件都负责使用saveStaterestoreState来保持其状态(请参阅本教程 )。 So different component suites result in different view state. 因此不同的组件套件会导致不同的视图状态。 Similarly, different JSF implementations might result in different view state. 同样,不同的JSF实现可能会导致不同的视图状态。

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. 视图技术(通常是JSP或Facelets)定义了初始状态,但之后它可以以编程方式进行操作。 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: 第一个定义组件树的结构:

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. 由于像UIData这样的组件,它们是分开的,其中子项可以(例如)每行状态。 This is marshalled/unmarshalled via the StateHolder mechanisms. 这是通过StateHolder机制编组/解组的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM