By GokiSoft.com| 20:08 23/04/2020|
Java Web + EA

Share Code - Full Source Code - Quản ly sinh viên - JSF (EA)

Source Code - Quản lý sinh viên - JSF

Persistence


<?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="C1803L-EAPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>entities.Student</class>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/c1803l"/>
      <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>



/*
 * 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 entities;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author DiepTV
 */
@Entity
@Table(name = "student")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Student.findAll", query = "SELECT s FROM Student s"),
    @NamedQuery(name = "Student.findByRollNo", query = "SELECT s FROM Student s WHERE s.rollNo = :rollNo"),
    @NamedQuery(name = "Student.findByFullname", query = "SELECT s FROM Student s WHERE s.fullname = :fullname"),
    @NamedQuery(name = "Student.findByEmail", query = "SELECT s FROM Student s WHERE s.email = :email"),
    @NamedQuery(name = "Student.findByAddress", query = "SELECT s FROM Student s WHERE s.address = :address"),
    @NamedQuery(name = "Student.findByGender", query = "SELECT s FROM Student s WHERE s.gender = :gender")})
public class Student implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 12)
    @Column(name = "roll_no")
    private String rollNo;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 150)
    @Column(name = "fullname")
    private String fullname;
    // @Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="Invalid email")//if the field contains email address consider using this annotation to enforce field validation
    @Size(max = 150)
    @Column(name = "email")
    private String email;
    @Size(max = 200)
    @Column(name = "address")
    private String address;
    @Size(max = 16)
    @Column(name = "gender")
    private String gender;

    public Student() {
    }

    public Student(String rollNo) {
        this.rollNo = rollNo;
    }

    public Student(String rollNo, String fullname) {
        this.rollNo = rollNo;
        this.fullname = fullname;
    }

    public String getRollNo() {
        return rollNo;
    }

    public void setRollNo(String rollNo) {
        this.rollNo = rollNo;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (rollNo != null ? rollNo.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Student)) {
            return false;
        }
        Student other = (Student) object;
        if ((this.rollNo == null && other.rollNo != null) || (this.rollNo != null && !this.rollNo.equals(other.rollNo))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entities.Student[ rollNo=" + rollNo + " ]";
    }
    
}



/*
 * 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 beans;

import entities.Student;
import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author DiepTV
 */
@ManagedBean(name = "studentBean")
public class StudentBean implements Serializable{
    Student deleteStd = null;
    Student student = null;
    
    public StudentBean() {
        student = new Student();
    }
    
    public List<Student> getStudentList() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("C1803L-EAPU");
        EntityManager em = factory.createEntityManager();
        
        Query q = em.createNamedQuery("Student.findAll", Student.class);
        
        return q.getResultList();
    }
    
    public String delete(String rollno) {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("C1803L-EAPU");
        EntityManager em = factory.createEntityManager();
        
        deleteStd = em.find(Student.class, rollno);
        
        if(deleteStd != null) {
            em.getTransaction().begin();
            em.remove(deleteStd);
            em.getTransaction().commit();
        }
        
        return "delete";
    }
    
    public Student getDeleteStd() {
        return deleteStd;
    }
    
    public Student getStudent() {
        return student;
    }
    
    public String goHome() {
        return "index";
    }
    
    public String redirectAddStudent() {
        return "addStudent";
    }
    
    public String redirectUpdateStudent(String rollno) {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("C1803L-EAPU");
        EntityManager em = factory.createEntityManager();
        
        student = em.find(Student.class, rollno);
        
        return "updateStudent";
    }
    
    public String saveStudent() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("C1803L-EAPU");
        EntityManager em = factory.createEntityManager();
        
        Student stdFind = em.find(Student.class, student.getRollNo());
        
        em.getTransaction().begin();
        if(stdFind == null) {
            em.persist(student);
        } else {
            //update
            stdFind.setAddress(student.getAddress());
            stdFind.setEmail(student.getEmail());
            stdFind.setFullname(student.getFullname());
            stdFind.setGender(student.getGender());
        }
        em.getTransaction().commit();
        
        student = new Student();
        
        return null;
    }
}



<?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>Student Manager</title>
    </h:head>
    <h:body>
        <h1>Student Manager</h1>
        
        <h:dataTable value="#{studentBean.studentList}" var="item" border="1">
            <h:column>
                <f:facet name="header">Roll No</f:facet>
                #{item.rollNo}
            </h:column>
            <h:column>
                <f:facet name="header">Full Name</f:facet>
                #{item.fullname}
            </h:column>
            <h:column>
                <f:facet name="header">Email</f:facet>
                #{item.email}
            </h:column>
            <h:column>
                <f:facet name="header">Gender</f:facet>
                #{item.gender}
            </h:column>
            <h:column>
                <f:facet name="header">Address</f:facet>
                #{item.address}
            </h:column>
            <h:column>
                <f:facet name="header">Edit</f:facet>
                <h:form>
                    <h:commandButton value="Edit" action="#{studentBean.redirectUpdateStudent(item.rollNo)}"/>
                </h:form>
            </h:column>
            <h:column>
                <f:facet name="header">Delete</f:facet>
                <h:form>
                    <h:commandButton value="Delete" action="#{studentBean.delete(item.rollNo)}"/>
                </h:form>
            </h:column>
        </h:dataTable>
        <h:form>
            <h:commandLink value="Add Student" action="#{studentBean.redirectAddStudent()}"/>
        </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>Notify to delete Student success</title>
    </h:head>
    <h:body>
        <h1>Student is deleted : (#{studentBean.deleteStd.fullname})</h1>
        <h:form>
            <h:commandLink value="Back Student List" action="#{studentBean.goHome()}"/>
        </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 Student</title>
    </h:head>
    <h:body>
        <h1>Add New Student</h1>
        <h:form>
            <p>
                <label>Roll No: <h:inputText value="#{studentBean.student.rollNo}"/></label>
            </p>
            <p>
                <label>Full Name: <h:inputText value="#{studentBean.student.fullname}"/></label>
            </p>
            <p>
                <label>Email: <h:inputText value="#{studentBean.student.email}"/></label>
            </p>
            <p>
                <label>Address: <h:inputText value="#{studentBean.student.address}"/></label>
            </p>
            <p>
                <label>Gender: <h:inputText value="#{studentBean.student.gender}"/></label>
            </p>
            <h:commandButton value="Submit" action="#{studentBean.saveStudent()}"/>
        </h:form>
        
        <h:form>
            <h:commandLink value="Back Student List" action="#{studentBean.goHome()}"/>
        </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>Update Student</title>
    </h:head>
    <h:body>
        <h1>Update New Student</h1>
        <h:form>
            <p>
                <label>Roll No: <h:inputText value="#{studentBean.student.rollNo}"/></label>
            </p>
            <p>
                <label>Full Name: <h:inputText value="#{studentBean.student.fullname}"/></label>
            </p>
            <p>
                <label>Email: <h:inputText value="#{studentBean.student.email}"/></label>
            </p>
            <p>
                <label>Address: <h:inputText value="#{studentBean.student.address}"/></label>
            </p>
            <p>
                <label>Gender: <h:inputText value="#{studentBean.student.gender}"/></label>
            </p>
            <h:commandButton value="Update" action="#{studentBean.saveStudent()}"/>
        </h:form>
        
        <h:form>
            <h:commandLink value="Back Student List" action="#{studentBean.goHome()}"/>
        </h:form>
    </h:body>
</html>



Tags:

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

5

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