|
1.Index.xhtml
<p><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" <br />
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"></p>
<p><html xmlns="http://www.w3.org/1999/xhtml"<br />
xmlns:ui="http://java.sun.com/jsf/facelets"<br />
xmlns:h="http://java.sun.com/jsf/html"<br />
xmlns:f="http://java.sun.com/jsf/core"><br />
<head><br />
<title>Dharmaraj.Net</title><br />
</head><br />
<body><br />
<h1>Book Manager Jsf Crud By Dharmaraj.Net</h1><br />
<h:dataTable var="book" value="#{bookController.listBook}" border="1" bgcolor="Silver"><br />
<h:column><br />
<f:facet name="header"><br />
<h:outputText value="Title"/><br />
</f:facet><br />
<h:outputText value="#{book.title}"/><br />
</h:column><br />
<br />
<h:column><br />
<f:facet name="header"><br />
<h:outputText value="Author"/><br />
</f:facet><br />
<h:outputText value="#{book.author}"/><br />
</h:column><br />
<br />
<h:column><br />
<f:facet name="header"><br />
<h:outputText value="Pages"/><br />
</f:facet><br />
<h:outputText value="#{book.pages}"/><br />
</h:column><br />
<br />
<h:column><br />
<f:facet name="header"><br />
<h:outputText value="Publishing House"/><br />
</f:facet><br />
<h:outputText value="#{book.publishingHouse}"/><br />
</h:column><br />
<br />
<h:column><br />
<f:facet name="header"><br />
<h:outputText value="ISBN"/><br />
</f:facet><br />
<h:outputText value="#{book.isbn}"/><br />
</h:column><br />
<br />
<h:column><br />
<f:facet name="header"><br />
<h:outputText value="Grading"/><br />
</f:facet><br />
<h:outputText value="#{book.grading}"/><br />
</h:column><br />
<br />
<h:column><br />
<f:facet name="header"><br />
<h:outputText value="Change"/><br />
</f:facet><br />
<h:form><br />
<h:commandButton action="#{bookController.prepareChangeBook}" value="Change"/><br />
</h:form><br />
</h:column><br />
<br />
<h:column><br />
<f:facet name="header"><br />
<h:outputText value="Delete"/><br />
</f:facet><br />
<h:form><br />
<h:commandButton action="#{bookController.deleteBook}" value="Delete"/><br />
</h:form><br />
</h:column><br />
<br />
</h:dataTable><br />
<br />
<br/><br />
<h:form><br />
<h:commandLink value="New Book" action="#{bookController.addBookToPrepare}"/><br />
</h:form> <br />
</body><br />
</html></p>
2.manageBook.xhtml
<p><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" <br />
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"></p>
<p><html xmlns="http://www.w3.org/1999/xhtml"<br />
xmlns:ui="http://java.sun.com/jsf/facelets"<br />
xmlns:h="http://java.sun.com/jsf/html"<br />
xmlns:f="http://java.sun.com/jsf/core"></p>
<p><head><br />
<title>Manage Books</title><br />
</head><br />
<h:body><br />
<h1>Add Book</h1><br />
<h:form><br />
<h:commandLink action="index" value="Return"/><br />
<h:panelGrid columns="2" border="1" bgcolor="Silver"><br />
Title <h:inputText value="#{bookController.book.title}"/> <br /><br />
Author <h:inputText value="#{bookController.book.author}"/> <br /><br />
Pages <h:inputText value="#{bookController.book.pages}"/> <br /><br />
PublishingHouse <h:inputText value="#{bookController.book.publishingHouse}"/> <br /><br />
ISBN <h:inputText value="#{bookController.book.isbn}"/> <br /><br />
Grading <h:selectOneMenu value="#{bookController.book.grading}"><br />
<f:selectItem itemLabel="1" itemValue="1"/><br />
<f:selectItem itemLabel="2" itemValue="2"/><br />
<f:selectItem itemLabel="3" itemValue="3"/><br />
<f:selectItem itemLabel="4" itemValue="4"/><br />
<f:selectItem itemLabel="5" itemValue="5"/><br />
</h:selectOneMenu><br />
<h:commandButton value="Add New Book" action="#{bookController.addBook}"/><br />
<h:commandButton value="change Book" action="#{bookController.changeBook}"/><br />
</h:panelGrid> <br />
</h:form><br />
</h:body><br />
</html></p>
3.HibernateUtil.java
<p>package Utils;</p>
<p>import org.hibernate.SessionFactory;<br />
import org.hibernate.cfg.AnnotationConfiguration;</p>
<p>import Model.Book;</p>
<p>public class HibernateUtil {<br />
private static SessionFactory sessionFactory;<br />
<br />
private HibernateUtil() {} // Constructor<br />
<br />
public static SessionFactory getSessionFactory(){<br />
if(sessionFactory == null){<br />
try {<br />
// Create the SessionFactory from standard (hibernate.cfg.xml)<br />
//config file.<br />
AnnotationConfiguration ac = new AnnotationConfiguration();<br />
ac.addAnnotatedClass(Book.class);<br />
sessionFactory = ac.configure().buildSessionFactory();<br />
} catch (Throwable ex) {<br />
System.err.println("Session Factory Error: "+ex);<br />
throw new ExceptionInInitializerError(ex);<br />
}//end of try catch block <br />
return sessionFactory;<br />
}else{<br />
return sessionFactory;<br />
}//end of else part<br />
}//end of Method<br />
}//end of Class<br />
</p>
4.Hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?><br />
<!DOCTYPE hibernate-configuration PUBLIC<br />
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"<br />
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><br />
<hibernate-configuration><br />
<session-factory><br />
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><br />
<property name="hibernate.connection.password">root</property><br />
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/database</property><br />
<property name="hibernate.connection.username">root</property><br />
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><br />
<property name="hibernate.hbm2ddl.auto">update</property><br />
<property name="show_sql">true</property><br />
</session-factory><br />
</hibernate-configuration><br />
5.BookBeanControler.java
<p>package Controller;</p>
<p>import java.util.List;</p>
<p>import javax.faces.model.DataModel;<br />
import javax.faces.model.ListDataModel;</p>
<p>import Dao.BookDao;<br />
import Dao.BookDaoImpl;<br />
import Model.Book;</p>
<p>public class BookBeanControler {<br />
private Book book;<br />
private DataModel listBook;<br />
//=========================================================<br />
public DataModel<Book> getListOfBook(){<br />
List<Book> books = new BookDaoImpl().listOfBooks();<br />
listBook = new ListDataModel<Book>(books);<br />
return listBook; <br />
}//end of Method<br />
//=========================================================<br />
public String addBookToPrepare(){<br />
book = new Book();<br />
return "manageBook";<br />
}//end of addBookToPrepare()<br />
//=========================================================<br />
public String prepareChangeBook(){<br />
book = (Book)(listBook.getRowData());<br />
return "manageBook";<br />
}//end of prepareChangeBook()<br />
//=========================================================<br />
public String deleteBook(){<br />
Book bookTemp = (Book)(listBook.getRowData());<br />
BookDao dao = new BookDaoImpl();<br />
dao.remove(bookTemp);<br />
return "index";<br />
}//end of deleteBook()<br />
//=========================================================<br />
public String addBook(){<br />
BookDao dao = new BookDaoImpl();<br />
dao.save(book);<br />
return "index";<br />
}//end of addBook()<br />
//=========================================================<br />
public String changeBook(){<br />
BookDao dao = new BookDaoImpl();<br />
dao.update(book);<br />
return "index";<br />
}//end of ChangeBook<br />
//=========================================================<br />
public Book getBook() {<br />
return book;<br />
}<br />
public void setBook(Book book) {<br />
this.book = book;<br />
}<br />
public DataModel getListBook() {<br />
List<Book> books = new BookDaoImpl().listOfBooks();<br />
listBook = new ListDataModel<Book>(books);<br />
return listBook; <br />
}<br />
public void setListBook(DataModel listBook) {<br />
this.listBook = listBook;<br />
}<br />
<br />
}//end of Class<br />
</p>
No comments:
Post a Comment