JSF : Page Forward vs Page Redirect

By default, JSF will performs a server page forward while navigating to another page. See following example to differentiate between page forward and page redirect.

A “start.xhtml” page, with a button navigate to “page1.xhtml” page.

1. Page Forward

Here’s how the page forward works :

  • Browser send a “GET” request to URL : http://localhost:8080/JavaServerFaces/faces/start.xhtml.
  • JSF received the request and return the “start.xhtml“.
  • Browser display the content of “start.xhtml“.
  • User click on the button.
    • JSF received the action and perform an internal page forward to “page1.xhtml” in the server side.
  • JSF return the “page1.xhtml“.
    • Browser display the content of the “page1.xhtml“.

In the page forward, browser’s URL is not update.
jsf2-page-forward-example

2. Page Redirection

Here’s how the page redirection works :

  • Browser send a “GET” request to URL : http://localhost:8080/JavaServerFaces/faces/start.xhtml.
  • JSF received the request and return the “start.xhtml“.
  • Browser display the content of “start.xhtml“.
  • User click on the button.
  • JSF received the action and send back a “redirect” to “page1.xhtml” response back to the browser.
  • Browser received the response and send another “GET” request to URL : http://localhost:8080/JavaServerFaces/faces/page1.xhtml.
  • JSF received the request and return the “page1.xhtml“.
  • Browser display the content of the “page1.xhtml“, and the browser’s URL is updated.

jsf2-page-redirection-example

To enable the page redirection in JSF 2.0, you can append “faces-redirect=true” at the end of the outcome string.

Page forward.

<h:form>
    <h:commandButton action="page1" value="Page1" />
</h:form>

Page redirection.




In the navigation rule, you can enable the page redirection by adding a `<redirect />` element within the `<navigation-case />`.


start.xhtml

page1
page1.xhtml



“`

Conclusion

Default page forward mechanism is more faster if compare to page redirection, because the page redirect added extra HTTP request to the server. So, only enable the page redirect when necessary, for example, uses Post/Redirect/Get Design Pattern to solve the classic duplicated form submission problem.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值