10

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

user1166528
  • 381
  • 3
  • 7
  • 18
  • Please explain a bit more. Do you need it for GET or POST requests? For every request in your project? Or is it a redirect in an action method? – Matt Handy Mar 07 '12 at 09:03
  • Thanks for your quick response.before loading the home page i want to check whether the link is having query string or not if query strin exists then i want to validate that query string and if it is not valid query string then i will redirect to error page if validation sucess then i will show home page. – user1166528 Mar 07 '12 at 09:06

1 Answers1

29

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555