Introduction
This simple application is a Create, Read, Update and Delete application operating on a ‘User’ table in ‘test’ database in MySQL Database Server. It is a Hibernate-Annotation based application.
Software Used
- JDK8u25
- Netbeans 8.02
- MySQL 5.*(or XAMPP)
- MySQL Connector 5.*
- Hibernate 4.3.** and Primefaces 5.0(Bundled with Netbeans)
Steps
- Install JDK8 or Jdk7 if not installed
- Install Netbeans and associated ApacheTomcat Server
- Install MySQL Database server or XAMPP(For easy management of MySQL ) .
After Installing Netbeans click the services tab on the left. Expand Database node. Expand Drivers node. Right click MySQL(Connector/Jdriver) then connect. Put test as the database, as shown below. Put password if you have given your password at the time of installation of MySQL database server. For XAMPP no password is required. Then test connection. If successful click finish button.
Create user table by running the below SQL in ‘test’ database.
- CREATE TABLE IF NOT EXISTS `user` (
- `id` int(10) unsigned NOT NULL auto_increment,
- `RecordNo` varchar(20) DEFAULT NULL,
- `Name` varchar(50) DEFAULT NULL,
- `Age` int(11) DEFAULT NULL,
- `Sex` varchar(20) DEFAULT NULL,
- `Dob` date DEFAULT NULL,
- `Remark` varchar(50) DEFAULT NULL PRIMARY KEY(`id`)
- ) ENGINE = InnoDB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8;
Insert these Records below by executing the below insert statement:
- INSERT INTO `user` (`id`, `RecordNo`, `Name`, `Age`, `Sex`, `Dob`, `Remark`) VALUES
- (1, '1', 'Raichand', 48, 'Male', '2014-06-16', 'Good Person'),
- (2, '2', 'Ramesh', 45, 'Male', '2013-03-18', 'Good Person'),
- (3, '3', 'smita', 47, 'Female', '1996-01-09', 'Jolly Lady'),
- (4, '4', 'Sarita', 37, 'Female', '1998-01-07', 'good beautiful lady'),
- (7, '7', 'Rashmi', 23, 'Male', '2016-01-25', 'good person'),
- (9, '9', 'Deepak', 21, 'Male', '2016-01-26', 'good person'),
- (10, '10', 'Jyoti Rath', 21, 'Male', '2016-01-26', 'good person'),
- (11, '11', 'Sruti Rath', 21, 'Male', '2016-01-26', 'good person'),
- (13, '13', 'Pradipta Prusty', 34, 'Male', '2016-01-18', 'good person'),
- (14, '14', 'sunita misra', 21, 'Female', '2016-01-27', 'good lady'),
- (15, '15', 'sibananda mitra', 32, 'Male', '2016-01-25', 'good person'),
- (16, '16', 'smita rath', 23, 'Female', '2016-01-20', 'good lady'),
- (17, '17', 'Smita satpathy', 21, 'Female', '2016-01-26', 'good lady'),
- (18, '18', 'N.C Das', 32, 'Male', '2016-01-26', 'Good Person'),
- (26, '19', 'sarita subudhi', 43, 'Female', '2016-01-26', 'good lady'),
- (27, '27', 'Ravi Mohanty', 44, 'Male', '2016-01-26', 'good person'),
- (28, '28', 'Sita', 23, 'Female', '2016-02-23', 'good lady');
Creating Project UserCRUD_Annotation
File->New Project->Categories->Choose JavaWeb->Choose WebApplication->Click Next->Give Project Name UserCRUD_Annotation->Click Next->Click Next->Choose Framework First Hibernate then Java Server Faces->Click Component Tab->Choose Primefaces->Click Finish.
Download mysql- connector-java-bin.jar and add to libraries folder by copying and pasting.
Project Structure
Creating Packages and Classes
Right click Source Package folder and create four packages:
- com.controller.bean: This would contain JSF Managed Bean Class UsererBean.java.
- com.dao: This would contain DAO(Data Access Object) class UsererDao.java.
- com.model.pojo: This would contain entity(POJO) class User.java.POJO Stands for Plain Old Java. Objects
- com.util: This will contain HibernateUtil.java class.
Default package contains two files hibernate.cfg.xml and hibernate.reveng.xml file:
Following Files would be created using Netbeans:
- hibernate.cfg,xml File: Automatically generated.
- Reverse Engineering File: hibernate.reveng.xml
- Entity(POJO) File: User.java(POJO stands for Plain Old Java Objects)
- JSF Managed Bean File: UserBean.java
- DataAccessObject(DAO) File: UserDao.java
- HibernateUtil.java File
- Index.xhtml(Automatically generated): Adds new user and displays all users information
- Search.xhtml: Search a user then edit or delete it
- UserCrud.xhtml: It is an independent file to be run independently by selecting it in TOC and running it by Run(From Menu): Run File
- web.xml(Automatically generated)
Add mysql- connector-java-bin.jar to libraries folder by copy and paste if not done.
Copy and paste code of the file given below whose code is not generated
1. Hibernate.cfg.xml File
As XAMPP is used so there is no password in the file only username is given that is root in Hibernate.cfg.xml File.
Code
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="hibernate.connection.url">jdbc:mysql:
- <property name="hibernate.connection.username">root</property>
- <property name="hibernate.connection.password"> </property>
- <property name="hibernate.connection.pool_size">10</property>
- <property name="show_sql">true</property>
- <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
- <property name="hibernate.hbm2ddl.auto">update</property>
- <mapping class="com.model.pojo.User" />
- <mapping class="com.model.pojo.User" />
- </session-factory>
- </hibernate-configuration>
2. Creating Reverse Engineering File->hibernate.reveng.xml
Right Click default package in the Source Package->new->choose Hibernate Reverse Engineering Wizard ->click next->choose customer table->Add ->click finish.
Code
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd">
- <hibernate-reverse-engineering>
- <schema-selection match-catalog="test" />
- <table-filter match-name="user" />
- </hibernate-reverse-engineering>
3. Creating Hibernate Entity (pojo) File:-user.java
Important: To create this file MySQL database test most be connected through Netbeans.
Right click com.model package->new->Hibernate Mappling Files and pojos from database -> Do not select mapping file & select EJB3.0 Pattern -> Click Finish
User.java Code
- package com.model.pojo;
-
- import java.util.Date;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import static javax.persistence.GenerationType.IDENTITY;
- import javax.persistence.Id;
- import javax.persistence.Table;
- import javax.persistence.Temporal;
- import javax.persistence.TemporalType;
-
-
-
- @Entity
- @Table(name = "user", catalog = "test")
- public class User implements java.io.Serializable
- {
- private Integer id;
- private String recordNo;
- private String name;
- private Integer age;
- private String sex;
- private Date dob;
- private String remark;
- public User()
- {}
- public User(String recordNo, String name, Integer age, String sex, Date dob, String remark)
- {
- this.recordNo = recordNo;
- this.name = name;
- this.age = age;
- this.sex = sex;
- this.dob = dob;
- this.remark = remark;
- }
- @Id @GeneratedValue(strategy = IDENTITY)
- @Column(name = "id", unique = true, nullable = false)
- public Integer getId()
- {
- return this.id;
- }
- public void setId(Integer id)
- {
- this.id = id;
- }
- @Column(name = "RecordNo", length = 20)
- public String getRecordNo()
- {
- return this.recordNo;
- }
- public void setRecordNo(String recordNo)
- {
- this.recordNo = recordNo;
- }
- @Column(name = "Name", length = 50)
- public String getName()
- {
- return this.name;
- }
- public void setName(String name)
- {
- this.name = name;
- }
- @Column(name = "Age")
- public Integer getAge()
- {
- return this.age;
- }
- public void setAge(Integer age)
- {
- this.age = age;
- }
- @Column(name = "Sex", length = 20)
- public String getSex()
- {
- return this.sex;
- }
- public void setSex(String sex)
- {
- this.sex = sex;
- }
- @Temporal(TemporalType.DATE)
- @Column(name = "Dob", length = 10)
- public Date getDob()
- {
- return this.dob;
- }
- public void setDob(Date dob)
- {
- this.dob = dob;
- }
- @Column(name = "Remark", length = 50)
- public String getRemark()
- {
- return this.remark;
- }
- public void setRemark(String remark)
- {
- this.remark = remark;
- }
-
- @Override
- public String toString()
- {
- return "user" +
- "\n\t RecordNo: " + this.recordNo +
- "\n\t EmployeeName: " + this.name +
- "\n\t Age: " + this.age +
- "\n\t Sex: " + this.sex +
- "\n\t Date of Birth: " + this.dob +
- "\n\t Remark: " + this.remark;
- }
- }
4. Creating JSF Managed Bean File
UserBean.java File
Right click com.controller package->new-> JSF Managed Bean ->Give class name UsererBean-> click finish.
UserBean.java File CODE
- package com.controller.bean;
- import javax.faces.bean.ManagedBean;
- import javax.faces.bean.ViewScoped;
- import java.util.List;
- import com.dao.UserDAO;
- import com.model.pojo.User;
- import java.io.Serializable;
- import org.primefaces.event.RowEditEvent;
- import javax.faces.context.FacesContext;
- import javax.faces.application.FacesMessage;
- import org.primefaces.context.RequestContext;
-
-
-
-
- @ManagedBean(name = "userBean")
- @ViewScoped
- public class UserBean implements Serializable
- {
- private List < User > usersList;
- private List < User > searchList;
- private List < User > searchByRecordNoList;
- UserDAO userDao = new UserDAO();
- User user = new User();
- User newuser = new User();
- public List < User > getUsers()
- {
- usersList = userDao.AllUsers();
- int count = usersList.size();
- return usersList;
- }
- public void addUser()
- {
- String Remark = newuser.getRemark();
- Integer userId = 0;
- userId = userDao.getId();
- newuser.setId(userId);
- String Id = Integer.toString(newuser.getId());
- newuser.setRecordNo(Integer.toString(userId));
- userDao.add(newuser);
- System.out.println("User successfully saved.");
- FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Save Information", "User successfully saved.");
- RequestContext.getCurrentInstance().showMessageInDialog(message);
- newuser = new User();
- }
- public void changeUser(User user)
- {
- this.user = user;
- }
- public void UpdateUser(User user)
- {
- String Name = user.getName();
- FacesMessage message1 = new FacesMessage(FacesMessage.SEVERITY_INFO, "Name", Name);
- RequestContext.getCurrentInstance().showMessageInDialog(message1);
- userDao.update(user);
- System.out.println("User Info successfully saved.");
- FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Save Information", "User updated successfully .");
- RequestContext.getCurrentInstance().showMessageInDialog(message);
- user = new User();
- }
- public void deleteUser(User user)
- {
- String Name = user.getName();
-
-
- userDao.delete(user);
- FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Delete", "Record deleted successfully");
- RequestContext.getCurrentInstance().showMessageInDialog(message);
- }
- public void searchbyRecordno()
- {
- searchByRecordNoList = userDao.SearchByRecordNo(user.getRecordNo());
- int count = searchByRecordNoList.size();
- FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Number of Record Selected:", Integer.toString(count));
- RequestContext.getCurrentInstance().showMessageInDialog(message);
- }
- public User getUser()
- {
- return user;
- }
- public void setUser(User user)
- {
- this.user = user;
- }
- public User getNewuser()
- {
- return newuser;
- }
- public void setNewuser(User newuser)
- {
- this.newuser = newuser;
- }
- public List < User > getUsersList()
- {
- return usersList;
- }
- public void setUsersList(List < User > usersList)
- {
- this.usersList = usersList;
- }
- public List < User > getSearchList()
- {
- return searchList;
- }
- public void setSearchList(List < User > searchList)
- {
- this.searchList = searchList;
- }
- public List < User > getSearchByRecordNoList()
- {
- return searchByRecordNoList;
- }
- public void setSearchByRecordNoList(List < User > searchByRecordNoList)
- {
- this.searchByRecordNoList = searchByRecordNoList;
- }
- public void onRowEdit(RowEditEvent event)
- {
- FacesMessage msg = new FacesMessage(" Edited Record No", ((User) event.getObject()).getRecordNo());
- FacesContext.getCurrentInstance().addMessage(null, msg);
- User editeduser = (User) event.getObject();
- userDao.update(editeduser);
- }
- public void onCancel(RowEditEvent event)
- {
- FacesMessage msg = new FacesMessage("Edit Cancelled");
- FacesContext.getCurrentInstance().addMessage(null, msg);
- usersList.remove((User) event.getObject());
- }
- }
5. Creating DataAccessObject (DAO) File
UserDAO.java File
Right click com.dao package->new->JavaClass->Give class name UserDAO-> click Finish.
UserDAO.java File Code
6. HibernateUtil.java File
Right click com.util folder-->new->javaclass->Class name ->Give name HibernateUtil ->Click Finish.
HibernateUtil.java Code
- package com.util;
- import java.util.Properties;
- import org.hibernate.SessionFactory;
- import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
- import org.hibernate.cfg.Configuration;
- import org.hibernate.service.ServiceRegistry;
- import com.model.pojo.User;
- public class HibernateUtil
- {
-
- private static SessionFactory sessionFactory;
- private static SessionFactory buildSessionFactory()
- {
- try
- {
-
- Configuration configuration = new Configuration();
- configuration.configure("/hibernate.cfg.xml");
- System.out.println("Hibernate Annotation Configuration loaded");
- ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
- System.out.println("Hibernate Annotation serviceRegistry created");
- sessionFactory = configuration.buildSessionFactory(serviceRegistry);
- return sessionFactory;
- }
- catch (Throwable ex)
- {
-
- System.err.println("Initial SessionFactory creation failed." + ex);
- throw new ExceptionInInitializerError(ex);
- }
- }
- public static SessionFactory getSessionFactory()
- {
- if (sessionFactory == null) sessionFactory = buildSessionFactory();
- return sessionFactory;
- }
- public static void shutdown()
- {
-
- sessionFactory.close();
- }
- }
7. Creating index.xhtml File
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <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>
- <h2><center>All User Information</center></h2>
- </h:head>
- <h:body>
- <h:form>
- <p:commandButton action="Search?faces-redirect=true" icon="ui-icon-search" value="Search" />
- </h:form>
-
- <p:spacer> </p:spacer>
- <h:form id="form1">
- <p:dataTable id="dataTable" var="user" paginator="true" rows="5" value="#{userBean.users}">
- <p:column headerText="RecordNo">
- <h:outputText value="#{user.recordNo}" />
- </p:column>
- <p:column headerText="Name">
- <h:outputText value="#{user.name}" />
- </p:column>
- <p:column sortBy="#{user.age}" headerText="Age">
- <h:outputText value="#{user.age}" />
-
- </p:column>
-
- <p:column headerText="Sex">
- <h:outputText value="#{user.sex}" />
- </p:column>
- <p:column headerText="Date of Birth">
- <h:outputText value="#{user.dob}">
- <f:convertDateTime type="date" pattern="dd-MMM-yyyy" />
- </h:outputText>
- </p:column>
- <p:column headerText="Remark">
- <h:outputText value="#{user.remark}" />
- </p:column>
- </p:dataTable>
- </h:form>
- <h:panelGroup>
- <h3>Add User Information</h3>
- <h:form>
- <p>User Name:
- <p:inputText value="#{userBean.newuser.name}" /></p>
- <p>User Age:
- <p:inputText value="#{userBean.newuser.age}" />Enter Number Only</p>
- <h:panelGrid columns="2" style="margin-bottom:10px" cellpadding="5">
- <p>Choose Sex:</p>
- <p:selectOneMenu label="Sex:" value="#{userBean.newuser.sex}" id="ulist2">
- <f:selectItem itemLabel="Select One" itemValue="" />
- <f:selectItem itemLabel="Male" itemValue="Male" />
- <f:selectItem itemLabel="Female" itemValue="Female" />
- </p:selectOneMenu>
- </h:panelGrid>
- <p>User Date of Birth:
- <p:calendar id="dop" value="#{userBean.newuser.dob}" label="DatePosted:" required="true" pattern="dd/MMM/yyyy" effect="slideDown" requiredMessage="Please Enter Date of Posting!" navigator="true" showButtonPanel="true" yearRange="c-60:c+60" /> </p>
- <p>Remark:
- <p:inputTextarea value="#{userBean.newuser.remark}" /></p>
- <p>
- <p:commandButton icon="ui-icon-plusthick" update=":form1:dataTable" type="submit" value="add" action="#{userBean.addUser()}" /></p>
- </h:form>
- </h:panelGroup>
- </h:body>
-
- </html>
8. Search.xhtml code
9. UserCRUD.xhtml
Under the webpages folder welcome.xhtml file is automatically generated.
Right click welcome.xhtml file->Refactor->rename->Givename UserCRUD->click finish.
It is a single page CRUD Project. It is an independent file. It can be run by selecting it in TOC then File(From Menu)->Run File.
UserCRUD.xhtml Code
- <?xml version='1.0' encoding='UTF-8' ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <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>User CRUD</title>
- </h:head>
- <h:body>
- <p:dialog id="userDetail1" widgetVar="$userDetail1" header="Add User" hideEffect="explode" appendTo="@(body)" resizable="false" draggable="false" closeOnEscape="true">
- <h:form>
-
- <p:panelGrid columns="2">
- <h:outputLabel for="username" value="User Name: *" />
- <p:inputText id="username" value="#{userBean.newuser.name}" label="User Name" placeholder="User Name" />
-
- <h:outputLabel for="userage" value="User Age" />
- <p:inputText id="userage" label="User Age" value="#{userBean.newuser.age}" placeholder="age" />
-
- <h:outputLabel for="sex" value="Choose Sex" />
- <p:selectOneMenu id="sex" label="Choose Sex" value="#{userBean.newuser.sex}" effect="fold">
- <f:selectItem itemLabel="Select One" itemValue="" />
- <f:selectItem itemLabel="Male" itemValue="Male" />
- <f:selectItem itemLabel="Female" itemValue="Female" />
- </p:selectOneMenu>
- <p:outputLabel for="dob" value="User Date of Birth" />
-
- <p:calendar id="dob" value="#{userBean.newuser.dob}" label="DatePosted:" required="true" pattern="dd/MM/yyyy" effect="slideDown" requiredMessage="Please Enter Date of Birth!" navigator="true" showButtonPanel="true" yearRange="c-60:c+60" placeholder="Date of Birth" />
-
- <p:outputLabel for="remark" value="Remark" />
- <p:inputTextarea id="remark" label="Remark" value="#{userBean.newuser.remark}" placeholder="Remark" />
-
- <p:commandButton value="add" process="@form" id="AddButtonId" ajax="true" icon="ui-icon-plus" update=":form1:userTable" actionListener="#{userBean.addUser()}" oncomplete="PF('$userDetail1').hide()" />
-
- <p:commandButton id="cancelAddButtonId" value="Cancel" onclick="handleComplete(xhr, status, args)" />
- </p:panelGrid>
- </h:form>
-
- </p:dialog>
- <h:outputScript>
-
-
- function handleComplete(xhr, status, args) {
- if(args && args.validName) {
- $userDetail1.hide();
- }
- }
-
-
- </h:outputScript>
- <p:dialog id="userDetail2" widgetVar="$userDetail2" header="Update User" hideEffect="explode" appendTo="@(body)" resizable="false" draggable="false" closeOnEscape="true">
- <h:form>
- <h:inputHidden value="#{userBean.user.id}" />
- <p:panelGrid columns="2">
- <h:outputLabel for="recordno" value="Record No: *" />
- <p:inputText id="recordno" value="#{userBean.user.recordNo}" label="Record No" placeholder="Record No" />
- <h:outputLabel for="username" value="User Name: *" />
- <p:inputText id="username" value="#{userBean.user.name}" label="User Name" placeholder="User Name" />
-
- <h:outputLabel for="userage" value="User Age" />
- <p:inputText id="userage" label="User Age" value="#{userBean.user.age}" placeholder="age" />
-
- <h:outputLabel for="sex" value="Choose Sex" />
- <p:selectOneMenu id="sex" label="Choose Sex" value="#{userBean.user.sex}" effect="fold">
- <f:selectItem itemLabel="Select One" itemValue="" />
- <f:selectItem itemLabel="Male" itemValue="Male" />
- <f:selectItem itemLabel="Female" itemValue="Female" />
- </p:selectOneMenu>
- <p:outputLabel for="dob" value="User Date of Birth" />
-
- <p:calendar id="dob" value="#{userBean.user.dob}" label="DatePosted:" required="true" pattern="dd/MM/yyyy" effect="slideDown" requiredMessage="Please Enter Date of Birth!" navigator="true" showButtonPanel="true" yearRange="c-60:c+60" placeholder="Date of Birth" />
-
- <p:outputLabel for="remark" value="Remark" />
- <p:inputTextarea id="remark" label="Remark" value="#{userBean.user.remark}" placeholder="Remark" />
-
- <p:commandButton value="Update" ajax="true" icon="ui-icon-disk" update=":form1:userTable" actionListener="#{userBean.UpdateUser(userBean.user)}" oncomplete="handleComplete(xhr, status, args)" />
- <p:commandButton value="Cancel" id="cancelButtonId" onclick="handleComplete(xhr, status, args)" />
- </p:panelGrid>
- </h:form>
-
- </p:dialog>
- <h:outputScript id="handleCompleteScript" target="body">
-
-
- function handleComplete(xhr, status, args) {
- if(args && args.validName) {
- $userDetail2.hide();
- }
- }
-
- </h:outputScript>
-
- <h:form id="form1">
- <p:commandButton icon="ui-icon-plusthick" id="addUserBtn" value="Add User" update=":userDetail1" ajax="true" oncomplete="PF('$userDetail1').show()" />
-
- <p:dataTable value="#{userBean.users}" var="user" paginator="true" rows="5" id="userTable">
- <p:column headerText="Record No" style="text-align: left;">
- <h:outputText value="#{user.recordNo}" />
- </p:column>
- <p:column headerText="Name">
- #{user.name}
- </p:column>
- <p:column sortBy="#{user.age}" headerText="Age">
- <h:outputText value="#{user.age}" />
-
- </p:column>
- <p:column headerText="Sex">
- <h:outputText value="#{user.sex}" />
- </p:column>
-
- <p:column headerText="Date of Birth">
- <h:outputText value="#{user.dob}">
- <f:convertDateTime type="date" pattern="dd-MMM-yyyy" />
- </h:outputText>
- </p:column>
- <p:column headerText="Remark">
- <h:outputText value="#{user.remark}" />
- </p:column>
- <p:column headerText="Edit" style="text-align: center">
- <p:commandButton icon="ui-icon-pencil" id="editUserBtn" value="Edit" ajax="true" actionListener="#{userBean.changeUser(user)}" update=":userDetail2" oncomplete="PF('$userDetail2').show()" />
-
- </p:column>
-
- <p:column headerText="Delete" style="text-align: center">
-
- <p:commandButton value="Delete" icon="ui-icon-trash" type="button" onclick="PF('confirmDialog').show()" />
- <p:confirmDialog message="Are you sure you want to delete this record?Record once deleted can not be retrieved." header="Deleting" severity="alert" widgetVar="confirmDialog">
- <p:commandButton value="Yes Sure" update=":form1:userTable" action="#{userBean.deleteUser(user)}" oncomplete="PF('confirmDialog').hide()" />
-
- <p:commandButton value="Not Yet" onclick="PF('confirmDialog').hide();" type="button" />
- </p:confirmDialog>
-
- </p:column>
- <p:rowExpansion>
- <h:outputText value="#{user.remark}" styleClass="rowExpansion" />
- </p:rowExpansion>
- </p:dataTable>
- </h:form>
-
- </h:body>
-
- </html>
10. web.xml (Automatically generated)
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
- <context-param>
- <param-name>javax.faces.PROJECT_STAGE</param-name>
- <param-value>Development</param-value>
- </context-param>
- <servlet>
- <servlet-name>Faces Servlet</servlet-name>
- <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>Faces Servlet</servlet-name>
- <url-pattern>*.xhtml</url-pattern>
- </servlet-mapping>
- <session-config>
- <session-timeout>
- 30
- </session-timeout>
- </session-config>
- <welcome-file-list>
- <welcome-file>index.xhtml</welcome-file>
- </welcome-file-list>
- </web-app>
Figure: Project Structure
Select the project in project window and run the project.
Index.xhtml page
Search.xhtml Page
Search.xhtml Page before deleting Record
UserCRUD.xhtml page
Select UserCRUD.xhtml page in TOC then choose File From Menuļ Run File