From Documentation

Jump to: navigation, search








Contents


A Web application jumps from one URL to another is usually caused by the user's click 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 on the server (in Java) too, such that you could redirect it based on some information that is available only at the server.

Redirect to Another URL

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

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

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

Redirect When Loading

Executions.sendRedirect(String) is designed to be used when serving an AU request (aka., Ajax). If you want to redirect to another page when loading a ZUML document, it is more efficient to call HttpServletResponse.sendRedirect[1], such that the browser will handle the redirect for you without running any JavaScript code.

For example,

if (!isLogin()) {
    Execution exec = Executions.getCurrent();
    HttpServletResponse response = (HttpServletResponse)exec.getNativeResponse();
    response.sendRedirect(response.encodeRedirectURL("/login")); //assume there is /login
    exec.setVoided(true); //no need to create UI since redirect will take place
}

Notice that we invoke Execution.setVoided(boolean) to void an execution, such that ZK Loader will abort the evaluation of a ZUML document (if you prefer not to generate any UI when redirecting).

Also notice that the casting to javax.servlet.http.HttpServletResponse in the above example does not work in a portlet, since the native response is an instance of javax.portlet.RenderResponse.

To check whether to redirect can be packed as Initiator, see below for an example:

public class AuthenticateInit extends org.zkoss.zk.ui.util.GenericInitiator {
    public void doInit(Page page, Map args) throws Exception {
        if (!isLogin()) {
            Execution exec = Executions.getCurrent();
            HttpServletResponse response = (HttpServletResponse)exec.getNativeResponse();
            response.sendRedirect(response.encodeRedirectURL("/login")); //assume there is /login
            exec.setVoided(true); //no need to create UI since redirect will take place
        }
    }
}

Then, you could specify it in your ZUML document:

<?init class="foo.AuthenticateInit"?>

  1. It actually sets the refresh header.

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 has logged in. For more information, please refer to the Conditional Evaluation section.

You could forward to another page by the 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 : 2014/10/30


Version Date Content
     



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



You got stuck here?
Let us know how we can improve this page
For specific questions please use the forum