JavaServer Faces (JSF) is a MVC web framework that simplifies the construction of User Interfaces (UI) for server-based applications using reusable UI components in a page. JSF provides a facility to connect UI widgets with data sources and to server-side event handlers. The JSF specification defines a set of standard UI components and provides an Application Programming Interface (API) for developing components. JSF enables the reuse and extension of the existing standard UI components.
This Project is basically a CRUD project where the four basic operation is performed on students database.
Index.html
<META http-equiv="refresh" content="0;URL=faces/list-students.xhtml">
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<f:metadata>
<f:event type="preRenderView" listener="#{studentController.loadStudents()}"/>
</f:metadata>
<h:head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Student Tracker App</title>
<h:outputStylesheet library="css" name="style.css" />
</h:head>
<h:body>
<div id="wrapper">
<div id="header">
<h2>FooBar University</h2>
</div>
</div>
<div id="container">
<div id="content">
<h:messages globalOnly="true" />
<h:button value="Add Student" styleClass="add-student-button" outcome="add-student-form"/>
<h:form>
<h:dataTable value="#{studentController.students}" var="tempStudent"
styleClass="demo-table"
headerClass="demo-table-header"
rowClasses="demo-table-odd-row,demo-table-even-row">
<h:column>
<!-- the column header -->
<f:facet name="header">First Name</f:facet>
<!-- the value for each row -->
#{tempStudent.firstName}
</h:column>
<h:column>
<!-- the column header -->
<f:facet name="header">Last Name</f:facet>
<!-- the value for each row -->
#{tempStudent.lastName}
</h:column>
<h:column>
<!-- the column header -->
<f:facet name="header">Email</f:facet>
<!-- the value for each row -->
#{tempStudent.email}
</h:column>
<h:column>
<!-- the column header -->
<f:facet name="header">Action</f:facet>
<!-- the value for each row -->
<h:commandLink value="Update"
action="#{studentController.loadStudent(tempStudent.id)}"/>
</h:column>
</h:dataTable>
</h:form>
</div>
</div>
</h:body>
</html>
add-student-form.xhtml
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add Student</title>
<h:outputStylesheet library="css" name="style.css" />
<h:outputStylesheet library="css" name="add-student-style.css" />
</h:head>
<h:body>
<div id="wrapper">
<div id="header">
<h2>FooBar University</h2>
</div>
</div>
<div id="container">
<h3>Add Student</h3>
<h:messages globalOnly="true" />
<h:form id="form" style="margin-top: 10px;" >
<h:panelGrid columns="3">
<h:outputLabel>First name:</h:outputLabel> <h:inputText value="#{student.firstName}" id="firstName" /> <h:message for="firstName" />
<h:outputLabel>Last name:</h:outputLabel> <h:inputText value="#{student.lastName}" id="lastName" /> <h:message for="lastName" />
<h:outputLabel>Email:</h:outputLabel> <h:inputText value="#{student.email}" id="email"
required="true"
requiredMessage="Your email is mandatory."/> <h:message for="email" />
<h:outputLabel /> <h:commandButton value="Save" styleClass="save" action="#{studentController.addStudent(student)}"/>
</h:panelGrid>
</h:form>
<div style="clear: both;"></div>
<p><h:outputLink value="list-students.xhtml">Back to List</h:outputLink></p>
</div>
</h:body>
</html>
update-student-form.xhtml
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Update Student</title>
<h:outputStylesheet library="css" name="style.css" />
<h:outputStylesheet library="css" name="add-student-style.css" />
</h:head>
<h:body>
<div id="wrapper">
<div id="header">
<h2>FooBar University</h2>
</div>
</div>
<div id="container">
<h3>Update Student</h3>
<h:messages globalOnly="true" />
<h:form id="form" style="margin-top: 10px;" >
<h:panelGrid columns="3">
<h:outputLabel>First name:</h:outputLabel> <h:inputText value="#{student.firstName}" id="firstName" /> <h:message for="firstName" />
<h:outputLabel>Last name:</h:outputLabel> <h:inputText value="#{student.lastName}" id="lastName" /> <h:message for="lastName" />
<h:outputLabel>Email:</h:outputLabel> <h:inputText value="#{student.email}" id="email"
required="true"
requiredMessage="Your email is mandatory."/> <h:message for="email" />
<h:inputHidden value="#{student.id}" />
<h:commandButton value="Save" styleClass="save"
action="#{studentController.updateStudent(student)}"/>
</h:panelGrid>
</h:form>
<div style="clear: both;"></div>
<p><h:outputLink value="list-students.xhtml">Back to List</h:outputLink></p>
</div>
</h:body>
</html>
studentController.java
package com.abdul;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
@ManagedBean
@SessionScoped
public class StudentController {
private List<Student> students;
private StudentDbUtil studentDbUtil;
private Logger logger = Logger.getLogger(getClass().getName());
public StudentController() throws Exception {
students = new ArrayList<>();
studentDbUtil = StudentDbUtil.getInstance();
}
public List<Student> getStudents() {
return students;
}
public void loadStudents() {
logger.info("Loading students");
students.clear();
try {
// get all students from database
students = studentDbUtil.getStudents();
} catch (Exception exc) {
// send this to server logs
logger.log(Level.SEVERE, "Error loading students", exc);
// add error message for JSF page
addErrorMessage(exc);
}
}
public String addStudent(Student theStudent) {
logger.info("Adding student: " + theStudent);
try {
// add student to the database
studentDbUtil.addStudent(theStudent);
} catch (Exception exc) {
// send this to server logs
logger.log(Level.SEVERE, "Error adding students", exc);
// add error message for JSF page
addErrorMessage(exc);
return null;
}
return "list-students?faces-redirect=true";
}
public String loadStudent(int studentId) {
logger.info("loading student: " + studentId);
try {
// get student from database
Student theStudent = studentDbUtil.getStudent(studentId);
// put in the request attribute ... so we can use it on the form page
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
Map<String, Object> requestMap = externalContext.getRequestMap();
requestMap.put("student", theStudent);
} catch (Exception exc) {
// send this to server logs
logger.log(Level.SEVERE, "Error loading student id:" + studentId, exc);
// add error message for JSF page
addErrorMessage(exc);
return null;
}
return "update-student-form.xhtml";
}
public String updateStudent(Student theStudent) {
logger.info("updating student: " + theStudent);
try {
// update student in the database
studentDbUtil.updateStudent(theStudent);
} catch (Exception exc) {
// send this to server logs
logger.log(Level.SEVERE, "Error updating student: " + theStudent, exc);
// add error message for JSF page
addErrorMessage(exc);
return null;
}
return "list-students?faces-redirect=true";
}
private void addErrorMessage(Exception exc) {
FacesMessage message = new FacesMessage("Error: " + exc.getMessage());
FacesContext.getCurrentInstance().addMessage(null, message);
}
}
StudentDButil.java
package com.abdul;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class StudentDbUtil {
private static StudentDbUtil instance;
private DataSource dataSource;
private String jndiName = "java:comp/env/jdbc/student_tracker";
public static StudentDbUtil getInstance() throws Exception {
if (instance == null) {
instance = new StudentDbUtil();
}
return instance;
}
private StudentDbUtil() throws Exception {
dataSource = getDataSource();
}
private DataSource getDataSource() throws NamingException {
Context context = new InitialContext();
DataSource theDataSource = (DataSource) context.lookup(jndiName);
return theDataSource;
}
public List<Student> getStudents() throws Exception {
List<Student> students = new ArrayList<>();
Connection myConn = null;
Statement myStmt = null;
ResultSet myRs = null;
try {
myConn = getConnection();
String sql = "select * from student order by last_name";
myStmt = myConn.createStatement();
myRs = myStmt.executeQuery(sql);
// process result set
while (myRs.next()) {
// retrieve data from result set row
int id = myRs.getInt("id");
String firstName = myRs.getString("first_name");
String lastName = myRs.getString("last_name");
String email = myRs.getString("email");
// create new student object
Student tempStudent = new Student(id, firstName, lastName, email);
// add it to the list of students
students.add(tempStudent);
}
return students;
} finally {
close(myConn, myStmt, myRs);
}
}
public void addStudent(Student theStudent) throws Exception {
Connection myConn = null;
PreparedStatement myStmt = null;
try {
myConn = getConnection();
String sql = "insert into student (first_name, last_name, email) values (?, ?, ?)";
myStmt = myConn.prepareStatement(sql);
// set params
myStmt.setString(1, theStudent.getFirstName());
myStmt.setString(2, theStudent.getLastName());
myStmt.setString(3, theStudent.getEmail());
myStmt.execute();
} finally {
close(myConn, myStmt);
}
}
public Student getStudent(int studentId) throws Exception {
Connection myConn = null;
PreparedStatement myStmt = null;
ResultSet myRs = null;
try {
myConn = getConnection();
String sql = "select * from student where id=?";
myStmt = myConn.prepareStatement(sql);
// set params
myStmt.setInt(1, studentId);
myRs = myStmt.executeQuery();
Student theStudent = null;
// retrieve data from result set row
if (myRs.next()) {
int id = myRs.getInt("id");
String firstName = myRs.getString("first_name");
String lastName = myRs.getString("last_name");
String email = myRs.getString("email");
theStudent = new Student(id, firstName, lastName, email);
} else {
throw new Exception("Could not find student id: " + studentId);
}
return theStudent;
} finally {
close(myConn, myStmt, myRs);
}
}
public void updateStudent(Student theStudent) throws Exception {
Connection myConn = null;
PreparedStatement myStmt = null;
try {
myConn = getConnection();
String sql = "update student " + " set first_name=?, last_name=?, email=?" + " where id=?";
myStmt = myConn.prepareStatement(sql);
// set params
myStmt.setString(1, theStudent.getFirstName());
myStmt.setString(2, theStudent.getLastName());
myStmt.setString(3, theStudent.getEmail());
myStmt.setInt(4, theStudent.getId());
myStmt.execute();
} finally {
close(myConn, myStmt);
}
}
public void deleteStudent(int studentId) throws Exception {
Connection myConn = null;
PreparedStatement myStmt = null;
try {
myConn = getConnection();
String sql = "delete from student where id=?";
myStmt = myConn.prepareStatement(sql);
// set params
myStmt.setInt(1, studentId);
myStmt.execute();
} finally {
close(myConn, myStmt);
}
}
private Connection getConnection() throws Exception {
Connection theConn = dataSource.getConnection();
return theConn;
}
private void close(Connection theConn, Statement theStmt) {
close(theConn, theStmt, null);
}
private void close(Connection theConn, Statement theStmt, ResultSet theRs) {
try {
if (theRs != null) {
theRs.close();
}
if (theStmt != null) {
theStmt.close();
}
if (theConn != null) {
theConn.close();
}
} catch (Exception exc) {
exc.printStackTrace();
}
}
}
Remember that there must be an existing database with you so that you
access it.
add-student-style.css
label {
font-size: 16px;
width: 100px;
display: block;
text-align: right;
margin-right: 10px;
margin-top: 8px;
margin-bottom: 8px;
}
input {
width: 250px;
border: 1px solid #666;
border-radius: 5px;
padding: 4px;
font-size: 16px;
}
.save {
font-weight: bold;
width: 130px;
padding: 5px 10px;
margin-top: 30px;
background: #cccccc;
}
style.css
html,body{
margin-left:15px; margin-right:15px;
padding:0px;
font-family:Verdana, Arial, Helvetica, sans-serif;
}
.demo-table {
border-collapse:collapse;
border-bottom:1px solid gray;
font-family: Tahoma,Verdana,Segoe,sans-serif;
width:72%;
}
.demo-table-header {
border-bottom:1px solid gray;
background:none repeat scroll 0 0 #0775d3;
padding:10px;
color: #FFFFFF;
}
.demo-table-odd-row {
border-top:1px solid gray;
background:none repeat scroll 0 0 #FFFFFFF;
text-align:center;
}
.demo-table-even-row {
border-top:1px solid gray;
background:none repeat scroll 0 0 #D0D0D0;
text-align:center;
}
#wrapper {width: 100%; margin-top: 0px; }
#header {width: 70%; background: #0775d3; margin-top: 0px; padding:15px 0px 15px 15px;}
#header h2 {width: 100%; margin:auto; color: #FFFFFF;}
#container {width: 100%; margin:auto}
#container h3 {color: #000;}
#container form#add input { background: #50acd6; border: 1px solid #666; border-radius: 5px; color: #fff; padding: 8px; font-size: 16px;}
#container form#find {margin-top:30px;}
#container form#find label {font-size: 13px; color: #000; float:left; margin-right: 20px;}
#container form#find input {border: 1px solid #ccc; border-radius: 3px; padding: 5px 10px; width: 86%;}
#container #content {margin-top: 20px;}
.add-student-button {
border: 1px solid #666;
border-radius: 5px;
padding: 4px;
font-size: 12px;
font-weight: bold;
width: 120px;
padding: 5px 10px;
margin-bottom: 15px;
background: #cccccc;
}
student.java
package com.abdul.jsf.jdbc;
import javax.faces.bean.ManagedBean;
@ManagedBean
public class Student {
private int id;
private String firstName;
private String lastName;
private String email;
public Student() {
}
public Student(int id, String firstName, String lastName, String email) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Student [id=" + id + ", firstName=" + firstName + ", lastName="
+ lastName + ", email=" + email + "]";
}
}
Screenshot b de dete project ka
ReplyDelete