java JSF:初始请求和回发请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2822883/
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
JSF: initial request and postback request?
提问by Thang Pham
Please take a look at this below line of code in JSF
请看下面这行 JSF 中的代码
<h:inputText id="name" value="#{customer.name}" />
Quote from java.sun.com:
引自 java.sun.com:
For an initial request of the page containing this tag, the JavaServer Faces implementation evaluates the
#{customer.name}expression during the render response phase of the lifecycle. During this phase, the expression merely accesses the value of name from the customer bean, as is done in immediate evaluation.For a postback request, the JavaServer Faces implementation evaluates the expression at different phases of the lifecycle, during which the value is retrieved from the request, validated, and propagated to the customer bean.
对于包含此标记的页面的初始请求,JavaServer Faces 实现
#{customer.name}在生命周期的呈现响应阶段评估表达式。在此阶段,表达式仅从客户 bean 访问 name 的值,就像在立即评估中所做的那样。对于回发请求,JavaServer Faces 实现在生命周期的不同阶段评估表达式,在此期间,值从请求中检索、验证并传播到客户 bean。
I am not sure I understand initial requestvs. postback request. Does the client browser make two different request to the webserver?
我不确定我是否理解初始请求与回发请求。客户端浏览器是否向网络服务器发出两个不同的请求?
采纳答案by Cesar
Initial request is the request that the browser does in order to display the page with the ${customer.name}tag. Postback happens when the browser posts some or all page values and then the same page that was posted in the first place is returned to the client. This might happen for example as a result of a validation error.
初始请求是浏览器为了显示带有${customer.name}标签的页面而执行的请求。当浏览器发布部分或所有页面值,然后将首先发布的相同页面返回给客户端时,就会发生回发。例如,这可能是由于验证错误而发生的。
Knowing if the current view being rendered is a result of a postback is useful. For example you might want to display a message as a result of a postback, but not every time the page is refreshed.
了解正在呈现的当前视图是否是回发的结果很有用。例如,您可能希望在回发后显示一条消息,但不是每次刷新页面时都显示。
回答by shwetank sharma
Initial request passes only Restore View & Render Response phases, while postback request process under all phases (Apply Request Values, Validations Phase, etc).
初始请求仅通过恢复视图和渲染响应阶段,而所有阶段(应用请求值、验证阶段等)下的回发请求过程。
Initial request is created by clicking a link, pasting an URL in address bar, while a postback request is create by posting a form by clicking a submit button or any post request.
初始请求是通过单击链接、在地址栏中粘贴 URL 来创建的,而回发请求是通过单击提交按钮或任何发布请求来发布表单来创建的。
回答by 99Sono
Normally you would have only one initial request, when you go to the browser and write in the URL to your app. This make an HTTP GET request to the server with your cookies e.g. JSESSIONID, but not with a javax.faces.viewid to be restored.
通常,当您转到浏览器并写入应用程序的 URL 时,您只有一个初始请求。这会使用您的 cookie(例如 JSESSIONID)向服务器发出 HTTP GET 请求,但不会使用要恢复的 javax.faces.viewid。
When you have an open page and you do hacky stuff lick: window.location = newUrl -> you will also make an initial request.
当你有一个打开的页面并且你做了一些 hacky 的事情 lick: window.location = newUrl -> 你也会提出一个初始请求。
When instead you do something like jQuery("#somoeSubmitButton").click(), you will POST to the server and your old view will be restored - and if you ask faces context.isPostback() ? you will get true.
相反,当您执行诸如 jQuery("#somoeSubmitButton").click() 之类的操作时,您将 POST 到服务器并且您的旧视图将被恢复 - 如果您询问 faces context.isPostback() ?你会明白的。

