Adding validation to your pages

You now have several options to validate your components. If you need a simple validation rule, you can use the basic f:validate options like in this example:
<h:inputText id="amount" value="#{person.amount}" >
      <f:validateLength maximum="10" />
If you need a more complex rule, you can demandate to your bean the validation:
 <h:inputText id="email" value="#{}"
      validator="#{personBean.validateEmail}" >

public void validateEmail(FacesContext context, UIComponent validated,Object value) {
          String mail = (String) value;        
          if (!mail.matches(".+\\@.+\\..+")) {            
              FacesMessage msg = new FacesMessage("This is not an e-mail!");              context.addMessage(validated.getClientId(context),msg);            throw new ValidatorException(msg);        

Better and simpler navigation

Another add-on in JSF 2.0, is that now supports implicit navigation, which can now be addresses directly in the JSP file, like in this example:
<h:commandButton value="Press me!" action="nextPage"/>

This will cause the JSF engine to invoke a page named nextPage in the same directory of the current page.
In our example, nextPage.jsp will be invoked. You can also specify full page name, if you have multiple pages with the same name in the same directory. 
<h:commandButton value="Press me!" action="nextPage.xhtml"/>

If the page where we need to navigate is located in another directory, you can obviously use the standard path convention:
<h:commandButton value="Press me!" action="/subdir/anotherPagel"/>
You might think that the file faces-config.xml is pretty useless at this point. It's not true, as a matter of fact an important add-on in JSF 2.0 is the ability to perform conditional navigation. By using conditional navigation you can get rid of navigations detail from your JSF Beans, where you will concentrate exclusively on the model that the Bean incarnates. 

Let's see a practical example:

public void action() {
    if (name != null && name.trim().length() > 0) 
        validationOk = true;
        validationOk = false;
In this example, instead of returning the next view from the method action() of the Class Person, we just set the property validationOk. Then the configuration file will decide where to navigate depending on the value of the property.

Finally, another opportunity offered by JSF 2 is the ability to issue a redirect when you invoke an action. The default action before now was to issue a forward, which is surely more efficient but sometimes you might need to redirect the navigation to a new page.
A typical example is where you have issues releated to the Refresh browser button, which might cause problems to your application if the address on the navigation bar is not consistent with the actual state of the application.

With JSF 2 you can just add the parameter faces-redirect=true to enable page redirection.
<h:commandLink  action="/sample?faces-redirect=true" value="Click"/>
Reference to next articles: