Thursday, November 24, 2011

Creating a database login screen using Primefaces and Hibernate

In this tutorial I will be creating a simple JSF login screen based on a database table. The final result will be a login form in which the user enters his username/password and it will redirect him to the home page or will show him a message indicating an invalid username/password or if his status is inactive.
In order to complete this tutorial you will need to complete those two tutorials, note that you will need to create both projects in the same workspace.

Additional steps that you will need to do are:
  1. Copy all the jar files found inside D:\workspaces\hibernate_lib to your primefaces web-inf/lib directory.
  2. Right click your dynamic web project and select deployment assembly, click on Add ->Project, click on next and then select JPAProject, click on finish, and then in your web deployment assembly screen click on OK.



After setting up our projects, we'll start by creating our business logic related classes and jsf pages.

  1. I will start by creating an enum with all the statuses of a login attempt, the enum looks like the following:
  2.  Then I will add a new Transient property to the BhUser class to hold the login status of the user
  3. Next I will create a UserDao interface and a UserDaoImpl class with one function called login which will take in a username and password and will return a BhUser object which will hold the login status within

    UserDao.java

    UserDaoImpl.java
  4. Create a request scoped managed bean called Login.java
  5. Create two xhtml pages, one for the login form and the other for your homepage

    WebContent/common/login.xhtml

    WebContent/common/home.xhtml
And the final result should be as follows


Offcourse you will need to have at least a single record in bh_user table to be able to test the application

4 comments:

  1. Hello,
    where am I going to put those packages with classes:
    - com.thebinaryheap.constants
    - com.thebinaryheap.model.entity
    - com.thebinaryheap.dao
    - com.thebinaryheap.bean.common
    is it in JPAProject or Primefaces? Please let me know!

    ReplyDelete
    Replies
    1. The first three packages belong to the JPA project
      - com.thebinaryheap.constants
      - com.thebinaryheap.model.entity
      - com.thebinaryheap.dao

      The fourth package which contains your managed beans belong to the dynamic web project
      - com.thebinaryheap.bean.common

      Delete
    2. Thank you for your quick answer. Now when I am trying to build Primefaces, I get the following errors even though the class Login does not show any errors.

      [javac] Compiling 1 source file to /Users/XXXXXXXXX/Documents/spring/Primefaces/WebContent/WEB-INF/classes
      [javac] /Users/XXXXXXXXX/Documents/spring/Primefaces/src/com/thebinaryheap/bean/common/Login.java:8: package com.thebinaryheap.constants does not exist
      [javac] import com.thebinaryheap.constants.LoginStatus;
      [javac] ^
      [javac] /Users/XXXXXXXXX/Documents/spring/Primefaces/src/com/thebinaryheap/bean/common/Login.java:9: package com.thebinaryheap.dao does not exist
      [javac] import com.thebinaryheap.dao.UserDao;
      [javac] ^
      [javac] /Users/XXXXXXXXX/Documents/spring/Primefaces/src/com/thebinaryheap/bean/common/Login.java:10: package com.thebinaryheap.dao does not exist
      [javac] import com.thebinaryheap.dao.UserDaoImpl;
      [javac] ^
      [javac] /Users/XXXXXXXXX/Documents/spring/Primefaces/src/com/thebinaryheap/bean/common/Login.java:11: package com.thebinaryheap.model.entity does not exist
      [javac] import com.thebinaryheap.model.entity.AuthUser;
      [javac] ^
      [javac] /Users/XXXXXXXXX/Documents/spring/Primefaces/src/com/thebinaryheap/bean/common/Login.java:26: cannot find symbol
      [javac] symbol : class UserDao
      [javac] location: class com.thebinaryheap.bean.common.Login
      [javac] UserDao userDao = new UserDaoImpl();
      [javac] ^
      [javac] /Users/XXXXXXXXX/Documents/spring/Primefaces/src/com/thebinaryheap/bean/common/Login.java:26: cannot find symbol
      [javac] symbol : class UserDaoImpl
      [javac] location: class com.thebinaryheap.bean.common.Login
      [javac] UserDao userDao = new UserDaoImpl();
      [javac] ^
      [javac] /Users/XXXXXXXXX/Documents/spring/Primefaces/src/com/thebinaryheap/bean/common/Login.java:27: cannot find symbol
      [javac] symbol : class AuthUser
      [javac] location: class com.thebinaryheap.bean.common.Login
      [javac] AuthUser user = userDao.login(username, password);
      [javac] ^
      [javac] /Users/XXXXXXXXX/Documents/spring/Primefaces/src/com/thebinaryheap/bean/common/Login.java:30: cannot find symbol
      [javac] symbol : variable LoginStatus
      [javac] location: class com.thebinaryheap.bean.common.Login
      [javac] if(user.getLoginStatus().equals(LoginStatus.SUCCESSFUL))
      [javac] ^
      [javac] /Users/XXXXXXXXX/Documents/spring/Primefaces/src/com/thebinaryheap/bean/common/Login.java:32: cannot find symbol
      [javac] symbol : variable LoginStatus
      [javac] location: class com.thebinaryheap.bean.common.Login
      [javac] else if(user.getLoginStatus().equals(LoginStatus.INACTIVE))
      [javac] ^
      [javac] 9 errors

      is it due to web assembly>

      Delete
    3. I believe these errors are there because your primefaces project is not seeing the jpa project, did you do the additional two steps I mentioned in the beginning of the tutorial to setup your projects? These two steps are there because so that you can use classes from your jpa project in your dynamic web project.

      Delete