I have a JSF 1.2 / Seam 2.2.2 application. I have button and on click of the button, I run validations on the objects in current page, query for some dependent objects and run validation. If the validation passes, then I need to redirect the user to the new page. Otherwise, I need to display validation errors in the same page. Below code works fine and error message is displayed. But how to I redirect to a new page?
<h:messages id="errorMsg" errorClass="errorMessages" />
<a4j:commandButton action="#{myController.shouldRedirectToNewPage()}"
value="Button" styleClass="button" />
public void shouldRedirectToNewPage() {
//Run validation on objects in the current page
//Query for dependent objects and run validation
if(validationFails) {
facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
"ERROR MESSAGE", null));
return;
}
}
Either use
ExternalContext#redirect():or return a navigation case string the usual way:
with the following in
faces-config.xml:(note the
<redirect/>, this makes it a redirect instead of a (default) forward)Unrelated to the concrete problem, doing validation inside the action method isn’t necessarily the best practice. It should be done by a
Validator. If it throws aValidatorException, then the action method simply won’t be invoked.