Forward and Redirect"

From Documentation
Line 33: Line 33:
 
You could forward to another page by use of <javadoc method="forward(java.lang.String)">org.zkoss.zk.ui.Executions</javadoc> too.
 
You could forward to another page by use of <javadoc method="forward(java.lang.String)">org.zkoss.zk.ui.Executions</javadoc> too.
  
Notice that forwarding can be called only when loading a page. You cannot call it when handling an Ajax request (such as a user clicks a button). Rather, you have to use ''redirect'' in an event listener for handling an Ajax request. Unlike redirect, forward does ''not'' change the URL that the browser knows. Rather, it is purely server-side activity: using another page's content instead of the original one to render the output of the given (and the same) URL.
+
Notice that forwarding can be called only when loading a page. You cannot call it when handling an Ajax request (such as when a user clicks a button). For handling an Ajax request, you have to use ''redirect'' as described in the previous section.
 +
 
 +
Unlike redirect, forward does ''not'' change the URL that the browser knows. Rather, it is purely server-side activity: using another page's content instead of the original one to render the output of the given (and the same) URL.
  
 
<blockquote>
 
<blockquote>

Revision as of 11:51, 19 November 2010


Forward and Redirect


A Web application's jumping from one URL to another is usually caused by user's clicking on a hyperlink, such as clicking on a button, toolbarbutton, menuitem and a that is associated with the href attribute.

<button label="Next" href="next.zul"/>

It is done at the client without Java code, so it is efficient. However, you could control it at the server (in Java) too, such that you could redirect based on some information that is available only at the server.

Redirect to Another URL

Redirecting to anther URL is straightforward: pass the URL to Executions.sendRedirect(String). A typical use case is to redirect after authenticating user's login.

if (someCondition())
   Executions.sendRedirect("/ready.zul");

You could also ask the browser to open another browser window for a given URL by use of Execution.sendRedirect(String, String).

Forward to Another Page

Sometimes we have to forward to another page. For example, when a user visits a page that requires authorization, we could forward it to a login page[1].

The simplest way is to use the forward directive:

<?forward uri="/login.zul" if="${!foo:isLogin()}"?>

where we assume isLogin is an EL function that returns whether the user logins. For more information, please refer to the forEach, if and unless section.

You could forward to another page by use of Executions.forward(String) too.

Notice that forwarding can be called only when loading a page. You cannot call it when handling an Ajax request (such as when a user clicks a button). For handling an Ajax request, you have to use redirect as described in the previous section.

Unlike redirect, forward does not change the URL that the browser knows. Rather, it is purely server-side activity: using another page's content instead of the original one to render the output of the given (and the same) URL.


  1. In additions to forwarding, we could popup a window to ask him to login, i.e., without leaving the current desktop

Version History

Last Update : 2010/11/19


Version Date Content
     



Last Update : 2010/11/19

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.