JSF Hibernate Integration (CRUD Example)

JSF Hibernate Integration (CRUD Example)
Code to integrate JSF with Hibernate and perform Create, Read, Update and Delete operations. This code is tested in Netbeans with MySQL Database.

Creating Project:
1) File –>New Project–>Java –>Web Application and click Next
2)Project name as HiberJSF and Click Next
3) Click Next
4) Choose the JSF and Hibernate framework and select the appropriate Database and click Finish

Structure
Structure

Table Creation

NewHibernateUtil.java

hibernate.reveng.xml

Student.hbm.xml

Student.java

Dao.java

index.xhtml

show.xhtml (to display all records)

Output:
Save:
save

ID Name Department
1 Alpha Computer Science
21 Seline Models
90 Sean Physics

Delete
delete

ID Name Department
21 Seline Models
90 Sean Physics

Show Record by ID
showid

Update
update

ID Name Department
21 Christy Planner

Show.xhtml

ID Name Department
21 Christy Planner
90 Sean Physics

 NOTE:
Do not forget to Add @ManagedBean in student.java and import – import javax.faces.bean.ManagedBean; (Not javax.annotation.ManagedBean)

 

 

 

 

 

 

 

 

 

Read More

Dynamic Navigation in JSF (Rule-Based Navigation)

Dynamic Navigation in JSF (Rule-Based Navigation)

What is Navigation:
A website is a collection of web pages. Moving between web pages of a website is known as Navigation. User can switch from one webpage to another in any of these following ways,

  • Clicking a button
  • Clicking Hyperlink
  • Typing URL

Types of Navigation:

  • Static Navigation
  • Dynamic Navigation

Dynamic Navigation:
When the decision to navigate from one webpage to another webpage that depends on the user input is known as Dynamic Navigation.

Dynamic Navigation is implemented using Rule-Based Navigation.

Navigation Rule:
This is the important part which plays a major role in Dynamic Navigation.

< navigation-rule > This is the root element that specifies how to navigate from one page to another
< navigation-case > Combination of conditions that must match for case to be executed
< from-view-id > Page where navigation originates
< from-action > Navigation criteria that must be satisfied for the case to execute
< from-outcome > Value to be compared with the output of < from-action >
< to-view-id > Depending on the outcome new page will be rendered

Note: Navigation-rule can contain any number of navigation cases.

Example:
Most websites have login page, when the user enters the login credentials and click login, the login credentials are validated.  If the login details are matched it will take you to home page or it will take you to error page. The user is moved to webpage based on his inputs. This is known as Dynamic Navigation.Lets consider a simple login page, When user enters the username as “alpha” and password as “alpha” the user will be navigated to Success.xhtml. If he enters wrong credentials he will be navigated to error.xhtml

JSF Managed Bean- customer.java

Success.xhtml

error.xhtml

login.xhtml

faces-config.xml

Explanation:
When user enters the details in login.xhtml page, if the username is “alpha” and password is “alpha”, the user will be navigated to success.xhtml or error.xhtml.

This action call the bean method “checkdetails()” which checks for the username and password validation.

Faces-config.xml:
From the example, lets start from Navigation rule. Since it is the root element Navigation-rule comes first
< from-view-id > : The navigation originates from login.xhtml page (i.e. When user enters the credentials he may be directed to success.xhtml or error.xhtml). so login.xhtml is mentioned in < from-view-id >
< from-action >: This is calling of method from bean.
< from-outcome >: In the bean method, we have defined possibility of 2 outcome’s, either “success” or “error”. So in from-outcome we have mentioned, if outcome is success then it goes to Success.xhtml.

In the second case,
< from-outcome >: If outcome from method checkdetails is error, it will navigate to error page.

Note: Navigation-rule can contain any number of navigation-case. In our example we have defined 2 navigation-case inside navigation-rule

Output:
dynamic1
outcome –>Success
dynamic2
Username is entered Wrong,
dynamic3
dynamic4

 

 

 

 

 

Read More

Navigation Model in JSF – Static Navigation

Navigation Model in JSF – Static Navigation

What is Navigation:
A website is a collection of web pages. Moving between web pages of a website is known as Navigation. User can switch from one webpage to another in any of these following ways,

  • Clicking a button
  • Clicking Hyperlink
  • Typing URL

Types of Navigation:

  • Static Navigation
  • Dynamic Navigation

Static Navigation:
Best example of Static Navigation is clicking on hyperlinks. A hyperlink may navigate the user to other webpages in the same website or different website. When the users click the hyperlink they are moved to the webpage linked to that hyperlink. This type of navigation, where user is moved to fixed webpage is known as Static Navigation.

Static Navigation Example: (Implicit Navigation)
page1.xhtml

page2.xhtml

Output:
static1
After clicking the link it navigates to page 2,
static2

Read More

Custom Validators using JSF

Custom Validators using JSF
The custom validators are user-defined validators that are created to meet application-specific validation. (For Standard Validators check here)

Custom Validators can be created in 2 ways

  • Implementing Validator Interface
  • Use Bean Method

Example: Lets create a JSF page to check if the person is eligible to vote using custom validators

Steps:
1. Create cvalidator java class and implement validator, write a function for validation
2. JSF Managed Bean
3. Register Custom validator class in faces-config.xml or using Annotations
4. Bind it with the UI component in JSF page

Implementing Validator Interface:
Create a java class and implement validator interface.

cvalidator.java

JSF Managed Bean – age.java

Registering Custom Validator class:
This can be done in 2 ways,

  • Register in faces-config.xml
  • using Annotations

Faces-config.xml:

Annotations:

index.xhtml


Bean Method:
JSF ManagedBean – age.java

index.xhtml

Output:


out1

 

 

 

 

 

 

 

 

 

 

Read More

Standard Validators in JSF

Standard Validators in JSF
The input provided by the user must be validated before processing it to the next step. Like checking the contact number, name should contain only alphabets etc.

JSF has built-in validators that is used to perform different types of DataValidation. These validators are known as Standard Validators and the tags are provided by Core tag library in JSF.

Standard Validators

 

validateDoubleRange validateLongRange validateLength validateRequired validateRegex
Used to validate Range of double inputs Used to validate Range of Long inputs Used to validate Length Similar to required attribute of UI Component Used to validate input against specified pattern

ValidatorMessage:
When we use these validators and if the user input is wrong, the system will show an error message to the user which is of system format and user wont be able to understand. To overcome this we use ValidatorMessage to give the error message in understandable format for the user.

Example:
Lets get the input from User and validate it using the above Standard Validators
Name – Should contain only Alphabets and no numbers
phone – Should have 10 numbers
Amount- Should be between 200 and 5000
Credit Card – Required Field

JSF Managed Bean – Customer.java

Input.xhtml

Output:
output1

 

 

Read More