Book Image

PrimeFaces Beginner's Guide

By : Siva Prasad Reddy Katamreddy
Book Image

PrimeFaces Beginner's Guide

By: Siva Prasad Reddy Katamreddy

Overview of this book

PrimeFaces is a lightweight UI component framework for JSF based applications. PrimeFaces is very easy to use because it comes as a single JAR file and requires no mandatory XML configuration. It provides more than 100 UI Components and an in-built AJAX support. It also provides theme support for UI components with more than 30 themes out-of-the-box. With PrimeFaces, developers can create rich user interfaces very easily.PrimeFaces Beginners Guide is a practical, hands-on guide that provides you with clear step-by-step exercises,that will help you to learn and explore the features of PrimeFaces.PrimeFaces Beginners Guide starts by showing you how to install PrimeFaces, create sample forms, and perform validations and then looks at various commonly used PrimeFaces utility components. Next, you will look into various basic text input components like form controls, Calendar, AutoComplete, and Rich Text Editor. Then you will learn about advanced UI components such as DataTables, panels, menus,and charts. Throughout the chapters we will be building a sample web application using PrimeFaces progressively that will give you a hands-on experience on using PrimeFaces effectively.You will learn how to create complex layouts using accordion panels, tab views, sophisticated menu navigations, breadcrumbs and much more. You will also learn how to display data using DataTable with pagination, filters, and lazy loading, and how to export data to Excel or PDF features. You will learn how to represent data in various formats like trees, charts, and TagCloud. You will also learn how to build an application supporting multiple themes.With this PrimeFaces Beginner's Guide , you will learn how to use PrimeFaces easily and effectively.
Table of Contents (20 chapters)
PrimeFaces Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
3
Using PrimeFaces Common Utility Components
Index

Time for action – validate e-mail using RemoteCommand


We will now take a look at how to invoke server-side logic from JavaScript code using RemoteCommand. First we will check the e-mail format on client side using JavaScript, and then invoke server-side logic to check if the user provided e-mail already in use or not. Perform the following steps:

  1. Create a method using following code in the UserController managed bean to check whether the given e-mail is already in use or not:

    @ManagedBean
    @RequestScoped
    public class UserController 
    {
      
      public void checkEmailExists()
      {
        String email = this.registrationUser.getEmail();
        if("[email protected]".equals(email) || "[email protected]".equals(email))
        {
          String msg = "Email ["+email+"] already in use.";
          FacesContext.getCurrentInstance().addMessage("registrationForm:email", 
              new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg));
        }
      }
    }
  2. On the user registration page, create validateEmail() JavaScript function for checking e-mail format, and use a remoteCommand component to invoke the checkEmailExists() actionListener method to check whether the given e-mail is already in use or not:

    <!DOCTYPE html> 
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:ui="http://java.sun.com/jsf/facelets"
          xmlns:p="http://primefaces.org/ui"> 
    
    <h:head>
      <title>Home</title>
      <script>
      function validateEmail() 
      {
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
        var email = $.trim($(PrimeFaces.escapeClientId("registrationForm:email")).val());
        if(email ==''){
          $(PrimeFaces.escapeClientId("registrationForm:emailMsg")).text("");
          $(PrimeFaces.escapeClientId("registrationForm:emailMsg")).attr("class", "");
          return;
        }
        if( emailReg.test( email ) ) {
          checkDuplicateEmail();       
        } else {
          $(PrimeFaces.escapeClientId("registrationForm:emailMsg")).text("Invalid Email");
          $(PrimeFaces.escapeClientId("registrationForm:emailMsg")).attr("class", "ui-message-error ui-widget ui-corner-all ui-message-error-detail");
         }
      }
      </script>
    </h:head> 
    <body> 
        
      <h:form id="registrationForm">
        <p:panel header="Registration Form" style="width: 800px;">    
          <h:panelGrid columns="3">
          
            <p:outputLabel value="Email:"/>
            <p:inputText id="email" value="#{userController.registrationUser.email}" onblur="validateEmail();" />
            <p:message id="emailMsg" for="email"/>
            
            <p:commandButton action="#{userController.register}" value="Register" update="registrationForm"/>  
            
          </h:panelGrid>
        </p:panel>
        <p:remoteCommand name="checkDuplicateEmail" actionListener="#{userController.checkEmailExists()}" update="emailMsg"/>    
      </h:form>    
    </body> 
    </html>

What just happened?

We have created a validateEmail() JavaScript function to check the e-mail format using Regex and will be called on onblur event on e-mail input element. In the validateEmail() function, if the e-mail format is invalid we are showing the error message as "Invalid email!!", otherwise we are invoking the remoteCommand checkDuplicateEmail as JavaScript function, which invokes the UserController.checkemailExists() method and add error message if the e-mail is already in use.