JSF: initial request and postback request?

こ雲淡風輕ζ 提交于 2021-02-17 09:38:12

问题


Please take a look at this below line of code in JSF

<h:inputText id="name" value="#{customer.name}" />

Quote from 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.

I am not sure I understand initial request vs. postback request. Does the client browser make two different request to the webserver?


回答1:


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.

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.




回答2:


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.




回答3:


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.

When you have an open page and you do hacky stuff lick: window.location = newUrl -> you will also make an initial request.

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.



来源:https://stackoverflow.com/questions/2822883/jsf-initial-request-and-postback-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!