I'm willing to call a method like this :
<h:commandButton value="Register" action="#{account.action}"/>
With a class like follow :
package com.sources;
public class Account {
private String password1;
private String password2;
public String getPassword1() {
return password1;
}
public void setPassword1(final String password1) {
this.password1 = password1;
}
public String getPassword2() {
return password2;
}
public void setPassword2(final String password2) {
this.password2 = password2;
}
public void action() {
//if the passwords matchs
//change page
//else
//display an error on the xhtml page
}
}
In the method, I would like to change the page or display an error, depending on the validity of the registration.
The action to change the page would be the same as follow, but called in the method #{account.action}
:
<h:commandButton value="Register" action="connect"/>
If you're using JSF-2, you can make use of implicit navigation:
public String action() {
if (password1 != null && password2 != null && password1.equals(password2)) {
return "connect";
} else {
FacesMessage msg = new FacesMessage("Passwords do not match");
FacesContext.getCurrentInstance().addMessage(null, msg);
return null;
}
}
This will navigate to a page connect.xhtml
if both passwords are equal. If they are not, the registration page will be re-rendered. To display the message, you need to add
<h:form>
<h:messages globalOnly="true" />
<h:inputText value="#{account.password1}" />
<h:inputText value="#{account.password2}" />
<h:commandButton value="Register" action="#{account.action()}" />
</h:form>
to your page.
See also:
Creating FacesMessage in action method outside JSF conversion/validation mechanism?