Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect before loading the page in JSF2 [duplicate]

Tags:

jsf-2

I have a requirement that before the page is going to load i want to check whether query string exists or not if query string exists then i want to redirect to another page instead of current page how can i handle this type of requirement in JSF 2.

Thanks in advance

like image 620
user1166528 Avatar asked Mar 07 '12 08:03

user1166528


1 Answers

When on JSF 2.2, you can use <f:viewAction> for this.

<f:metadata>
    <f:viewParam name="paramName" value="#{bean.paramName}" />
    <f:viewAction action="#{bean.check}" />
</f:metadata>

(paramName is the name of your query string parameter)

private String paramName; // +getter+setter

public String check() {
    if (paramName == null) {
        return "error.xhtml";
    }

    return null;
}

When not on JSF 2.2 yet (JSF 2.0/2.1), you can use <f:event type="preRenderView"> for this.

<f:metadata>
    <f:viewParam name="paramName" value="#{bean.paramName}" />
    <f:event type="preRenderView" listener="#{bean.check}" />
</f:metadata>
private String paramName; // +getter+setter

public void check() throws IOException {
    if (paramName == null) {
        FacesContext.getCurrentInstance().getExternalContext().redirect("error.xhtml");
    }
}

See also:

  • What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?
like image 68
BalusC Avatar answered Dec 02 '22 23:12

BalusC