Reader small image

You're reading from  PrimeFaces Beginner's Guide

Product typeBook
Published inNov 2013
Reading LevelIntermediate
PublisherPackt
ISBN-139781783280698
Edition1st Edition
Languages
Right arrow
Author (1)
Siva Prasad Reddy Katamreddy
Siva Prasad Reddy Katamreddy
author image
Siva Prasad Reddy Katamreddy

K. Siva Prasad Reddy is a Senior Software Engineer living in Hyderabad, India, and having more than seven years of experience in developing enterprise applications with Java and JavaEE technologies. Siva is a Sun Certified Java Programmer and has a lot of experience in server-side technologies such as Java, JavaEE, Spring, Hibernate, MyBatis, JSF, PrimeFaces, and WebServices (SOAP/REST). Siva is also the author of Java Persistence with MyBatis 3, Packt Publishing. Siva normally shares the knowledge he has acquired on his blog at www.sivalabs.in. If you want to find out more about his work, you can follow him on Twitter (@sivalabs) and GitHub (https://github.com/sivaprasadreddy).
Read more about Siva Prasad Reddy Katamreddy

Right arrow

Time for action – updating the view using AJAX


Let us create a Login Form with user name and password. When form is submitted, show the login status using AJAX. Perform the following steps for the same:

  1. Create a login.xhtml page with login form as follows:

    <h:form id="loginForm">
      <p:panel header="Login Form" style="width: 500px;">  
        <h:panelGrid columns="2">
          <p:outputLabel value="UserName"/>
          <p:inputText value="#{userController.loginUser.userName}"/>
          
          <p:outputLabel value="Password"/>
          <p:password value="#{userController.loginUser.password}"/>
          
          <p:commandButton action="#{userController.login}" value="Login" update="loginStatusMsg"/>
          <p:commandButton type="reset" value="Reset"/>
          
          <p:outputLabel value="#{userController.loginStatus}" id="loginStatusMsg"/>
        </h:panelGrid>
      </p:panel>
    </h:form>
  2. Create a UserController.java managed bean as follows:

    @ManagedBean
    @RequestScoped
    public class UserController 
    {
      private User loginUser;
      private String loginStatus;
      
      public UserController() {
        this.loginUser = new User();    
      }
    
      public User getLoginUser() {
        return loginUser;
      }
    
      public void setLoginUser(User loginUser) {
        this.loginUser = loginUser;
      }
    
      public String getLoginStatus() {
        return loginStatus;
      }
      
      public void setLoginStatus(String loginStatus) {
        this.loginStatus = loginStatus;
      }
      
      public String login() {
        boolean validCredentials = "admin".equals(loginUser.getUserName()) && "admin".equals(loginUser.getPassword());
        this.loginStatus  = validCredentials? "Login Successful" : "Login failed";
        return null;
      }    
    }
  3. Point the browser to http://localhost:8080/chapter01/login.jsf.

When you enter UserName and Password and click on the Login button, the login status should be displayed using AJAX. Have a look at the following screenshot:

What just happened?

We have created a Login Form with UserName and Password fields. When we submit the form, the model gets updated and login status will be set based on the provided credentials. As we have specified to update the view component with the ID loginStatusMsg through update="loginStatusMsg", when you click on the Login button, the login status will be displayed without complete page refresh.

Previous PageNext Page
You have been reading a chapter from
PrimeFaces Beginner's Guide
Published in: Nov 2013Publisher: PacktISBN-13: 9781783280698
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime

Author (1)

author image
Siva Prasad Reddy Katamreddy

K. Siva Prasad Reddy is a Senior Software Engineer living in Hyderabad, India, and having more than seven years of experience in developing enterprise applications with Java and JavaEE technologies. Siva is a Sun Certified Java Programmer and has a lot of experience in server-side technologies such as Java, JavaEE, Spring, Hibernate, MyBatis, JSF, PrimeFaces, and WebServices (SOAP/REST). Siva is also the author of Java Persistence with MyBatis 3, Packt Publishing. Siva normally shares the knowledge he has acquired on his blog at www.sivalabs.in. If you want to find out more about his work, you can follow him on Twitter (@sivalabs) and GitHub (https://github.com/sivaprasadreddy).
Read more about Siva Prasad Reddy Katamreddy