By GokiSoft.com| 15:04 25/03/2020|
Java Web + EJB (EAD)

[Share Code] Quản lý sách + tác giả EJB + Web

Hướng dẫn giải dự án

EJB-EAD- Viết chương trình quản lý sách

EJB



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package session;

import entities.Book;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author DiepTV
 */
@Stateless
public class BookSessionBean implements BookSessionBeanLocal {

    @Override
    public List<Book> findAll() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("BookWeb-ejbPU");
        EntityManager em = factory.createEntityManager();
        
        Query q = em.createNamedQuery("Book.findAll", Book.class);
        
        return q.getResultList();
    }

    @Override
    public void insert(Book book) {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("BookWeb-ejbPU");
        EntityManager em = factory.createEntityManager();
        
        em.getTransaction().begin();
        em.persist(book);
        em.getTransaction().commit();
    }

    @Override
    public void delete(String bookCode) {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("BookWeb-ejbPU");
        EntityManager em = factory.createEntityManager();
        
        Book book = em.find(Book.class, bookCode);
        
        em.getTransaction().begin();
        em.remove(book);
        em.getTransaction().commit();
    }

    @Override
    public void update(Book book) {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("BookWeb-ejbPU");
        EntityManager em = factory.createEntityManager();
        
        Book bookFind = em.find(Book.class, book.getBookcode());
        
        em.getTransaction().begin();
        bookFind.setName(book.getName());
        bookFind.setNickname(book.getNickname());
        bookFind.setNxb(book.getNxb());
        bookFind.setPrice(book.getPrice());
        em.getTransaction().commit();
    }

    @Override
    public Book find(String bookCode) {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("BookWeb-ejbPU");
        EntityManager em = factory.createEntityManager();
        
        return em.find(Book.class, bookCode);
    }
    
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package session;

import entities.Book;
import java.util.List;
import javax.ejb.Local;

/**
 *
 * @author DiepTV
 */
@Local
public interface BookSessionBeanLocal {
    List<Book> findAll();
    
    void insert(Book book);
    
    void delete(String bookCode);
    
    void update(Book book);
    
    Book find(String bookCode);
}



<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="BookWeb-ejbPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>entities.Book</class>
    <class>entities.Author</class>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/book"/>
      <property name="javax.persistence.jdbc.user" value="root"/>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
      <property name="javax.persistence.jdbc.password" value=""/>
    </properties>
  </persistence-unit>
</persistence>

WEB



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import entities.Book;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import session.BookSessionBeanLocal;

/**
 *
 * @author DiepTV
 */
@ManagedBean(name = "bookBean")
public class BookBean implements Serializable{
    @EJB
    private BookSessionBeanLocal bookSessionBean;
    private Book book;
    
    public BookBean() {
        this.book = new Book();
    }
    
    public List<Book> getBookList() {
        return bookSessionBean.findAll();
    }
    
    public void delete(String bookCode) {
        bookSessionBean.delete(bookCode);
    }
    
    public String edit(String bookCode) {
        book = bookSessionBean.find(bookCode);
        return "editbook";
    }
    
    public Book getBook() {
        return book;
    }
    
    public void insert() {
        bookSessionBean.insert(book);
    }
    
    public void update() {
        bookSessionBean.update(book);
    }
}



<?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://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>Add Book</title>
    </h:head>
    <h:body>
        Add Book
        <h:form>
            <p>
                Book Code <h:inputText value="#{bookBean.book.bookcode}"/>
            </p>
            <p>
                Book Name: <h:inputText value="#{bookBean.book.name}"/>
            </p>
            <p>
                Nick Name: <h:inputText value="#{bookBean.book.nickname}"/>
            </p>
            <p>
                NXB: <h:inputText value="#{bookBean.book.nxb}"/>
            </p>
            <p>
                Price: <h:inputText value="#{bookBean.book.price}"/>
            </p>
            <h:commandButton action="#{bookBean.insert()}" value="Save"/>
        </h:form>
    </h:body>
</html>




<?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://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>Add Book</title>
    </h:head>
    <h:body>
        Add Book
        <h:form>
            <p>
                Book Code <h:inputText value="#{bookBean.book.bookcode}"/>
            </p>
            <p>
                Book Name: <h:inputText value="#{bookBean.book.name}"/>
            </p>
            <p>
                Nick Name: <h:inputText value="#{bookBean.book.nickname}"/>
            </p>
            <p>
                NXB: <h:inputText value="#{bookBean.book.nxb}"/>
            </p>
            <p>
                Price: <h:inputText value="#{bookBean.book.price}"/>
            </p>
            <h:commandButton action="#{bookBean.update()}" value="Save"/>
        </h:form>
    </h:body>
</html>




<?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://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
        <title>Book List</title>
    </h:head>
    <h:body>
        Book List
        
        <h:dataTable border="1" value="#{bookBean.bookList}" var="item">
            <h:column>
                <f:facet name="header">Book Name</f:facet>
                #{item.name}
            </h:column>
            <h:column>
                <f:facet name="header">Nick Name</f:facet>
                #{item.nickname}
            </h:column>
            <h:column>
                <f:facet name="header">NXB</f:facet>
                #{item.nxb}
            </h:column>
            <h:column>
                <f:facet name="header">Price</f:facet>
                #{item.price}
            </h:column>
            <h:column>
                <f:facet name="header">Delete</f:facet>
                <h:form>
                    <h:commandButton action="#{bookBean.delete(item.bookcode)}" value="Delete"></h:commandButton>
                </h:form>
            </h:column>
            <h:column>
                <f:facet name="header">Edit</f:facet>
                <h:form>
                    <h:commandButton action="#{bookBean.edit(item.bookcode)}" value="Edit"></h:commandButton>
                </h:form>
            </h:column>
        </h:dataTable>
        <a href="addbook.xhtml" target="_blank">Add Book</a>
    </h:body>
</html>



Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)