Redirect


1. 用户浏览器向 http://localhost:8080/demo/start.xhtml 发送 GET 请求。


2. JSF收到请求,返回 start.xhtml 页面。


3. 用户点击页面中的按钮。


4. JSF收到请求, 向浏览器发送 Redirect 指令(3XX的HTTP状态值)。


5. 浏览器收到指令, 发送另一个 GET 到 http://localhost:8080/demo/page1.xhtml。


6.JSF返回page1.xhtml。


7. 浏览器显示page1.xhtml。这时地址栏里显示的是page1.xhtml。



Forward



1. 用户浏览器向 http://localhost:8080/demo/start.xhtml 发送 GET 请求。



2. JSF收到请求,返回 start.xhtml 页面。



3. 用户点击页面中的按钮。



4. JSF收到请求, 直接渲染page1.xhtml页面并返回给浏览器



5. 浏览器显示 page1.xhtml。这时候地址栏里显示的还是start.xhtml









由此可看出,重定向要比跳转多发送一个请求,所以相对要慢一些。JSF默认全部采用 Forward的方式跳转页面。如果想要重定向页面,可以在导航中添加 <redirect />,或者在 h:commandButton的action属性后添加 "faces-redirect=true":



    1. <h:form>  
    2. <h:commandButton action="page1?faces-redirect=true" value="Page1" />  
    3. </h:form>  
    4.