Use <f:viewAction>
to trigger a bean method before rendering of the view and simply return a navigation outcome (which will implicitly be treated as a redirect).
E.g.
<f:metadata>
<f:viewParam name="token" value="#{authenticator.token}" />
<f:viewAction action="#{authenticator.check}" />
</f:metadata>
with
@ManagedBean
@RequestScoped
public class Authenticator {
private String token;
public String check() {
return isValid(token) ? null : "main.jsf";
}
// Getter/setter.
}
If you’re not on JSF 2.2 yet, then you can use the <f:event type="preRenderView">
workaround in combination with ExternalContext#redirect()
.
<f:metadata>
<f:viewParam name="token" value="#{authenticator.token}" />
<f:event type="preRenderView" listener="#{authenticator.check}" />
</f:metadata>
with
@ManagedBean
@RequestScoped
public class Authenticator {
private String token;
public void check() throws IOException {
if (!isValid(token)) {
FacesContext.getCurrentInstance().getExternalContext().redirect("main.jsf");
}
}
// Getter/setter.
}