Forward and Redirect"

From Documentation
Line 1: Line 1:
 
{{ZKDevelopersReferencePageHeader}}
 
{{ZKDevelopersReferencePageHeader}}
  
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 [[ZK Component Reference/Essential Components/Button|button]], [[ZK Component Reference/Essential Components/Toolbarbutton|toolbarbutton]], [[ZK Component Reference/Essential Components/Menu/Menuitem|menuitem]] and [[ZK Component Reference/Essential Components/A|a]] that is associated with the <tt>href</tt> attribute. It is done at the client without Java code.
+
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 [[ZK Component Reference/Essential Components/Button|button]], [[ZK Component Reference/Essential Components/Toolbarbutton|toolbarbutton]], [[ZK Component Reference/Essential Components/Menu/Menuitem|menuitem]] and [[ZK Component Reference/Essential Components/A|a]] that is associated with the <tt>href</tt> attribute.
  
However, you could control it at the server (in Java) too.
+
<source lang="xml">
 +
<button label="Next" href="next.zul"/>
 +
</source>
 +
 
 +
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=
 
=Redirect to Another URL=
  
=Forward to Another URL=
+
Redirecting to anther URL is straightforward: pass the URL to <javadoc method="sendRedirect(java.lang.String)">org.zkoss.zk.ui.Executions</javadoc>. A typical use case is to redirect after authenticating user's login.
 +
 
 +
<source lang="java">
 +
if (someCondition())
 +
  Executions.sendRedirect("/ready.zul");
 +
</source>
 +
 
 +
You could also ask the browser to open another browser window for a given URL by use of <javadoc method="sendRedirect(java.lang.String, java.lang.String)" type="interface">org.zkoss.zk.ui.Execution</javadoc>.
 +
 
 +
=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<ref>In additions to forwarding, we could popup a window to ask him to login, i.e., without leaving the current desktop</ref>.
 +
 
 +
The simplest way is to use the [[ZUML Reference/ZUML/Processing Instructions/forward|forward directive]]:
 +
<source lang="xml">
 +
<?forward uri="/login.zul" if="${!foo:isLogin()}"?>
 +
<blockquote>
 +
 
 +
where we assume <tt>isLogin</tt> is an EL function that returns whether the user logins. For more information, please refer to the [[ZK Developer's Reference/UI Composing/ZUML/forEach, if and unless|forEach, if and unless]] section.
 +
 
 +
----
 +
<references/>
 +
</blockquote>
 +
 
 
=Version History=
 
=Version History=
 
{{LastUpdated}}
 
{{LastUpdated}}

Revision as of 11:45, 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: <source lang="xml"> <?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.


  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.