我不能为我的生活弄清楚代码的错误。我花了一些时间试图调试这个问题。当我填写添加员工表单时,只在表单中创建空值。在这一点上,我陷入困境,需要一些帮助才能找到解决方案。
EmpController.java
package ejb605.controller.com;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import ejb605.assignment2.com.EmployeeManager;
import entity.Employee;
@ManagedBean
@SessionScoped
public class EmpController implements Serializable{
@EJB
EmployeeManager em;
private List<Employee> list = new ArrayList<>();
private Employee emp = new Employee();
private String empSearchID;
public List<Employee> getList() {
list = em.getAllEmployees();
return list;
}
public String getEmpSearchID() {
return empSearchID;
}
public void setEmpSearchID(String empSearchID) {
this.empSearchID = empSearchID;
}
public Employee getEmp() {
return emp;
}
public void setEmp(Employee emp) {
this.emp = emp;
}
public String Search(){
this.setEmp(em.getEmployee(Integer.parseInt(this.empSearchID)));
return "EmployeeSearchDetails";
}
public String AddEmp(){
emp = em.addEmployee(this.emp);
list = em.getAllEmployees();
return "NewEmployee";
}
}
Employee.java JPA
package entity;
import java.io.Serializable;
import javax.persistence.*;
import java.util.Date;
/**
* The persistent class for the employees database table.
*
*/
@Entity
@Table(name="employees")
@NamedQuery(name="findAllEmployees", query="SELECT e FROM Employee e")
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Column(name="department_id")
private int departmentId;
private String email;
@Column(name="first_name")
private String firstName;
@Temporal(TemporalType.DATE)
@Column(name="hire_date")
private Date hireDate;
@Column(name="last_name")
private String lastName;
@Column(name="manager_id")
private int managerId;
private String phone;
public Employee() {
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public int getDepartmentId() {
return this.departmentId;
}
public void setDepartmentId(int departmentId) {
this.departmentId = departmentId;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public Date getHireDate() {
return this.hireDate;
}
public void setHireDate(Date hireDate) {
this.hireDate = hireDate;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getManagerId() {
return this.managerId;
}
public void setManagerId(int managerId) {
this.managerId = managerId;
}
public String getPhone() {
return this.phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
package ejb605.assignment2.com;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import entity.Employee;
/**
* Session Bean implementation class EmployeeManager
*/
@Stateless
@LocalBean
public class EmployeeManager implements EmployeeManagerLocal {
@PersistenceContext(name="EmployeeJPA")
EntityManager em;
public EmployeeManager(){
}
public List<Employee> getAllEmployees() {
Query q = em.createNamedQuery("findAllEmployees",Employee.class);
return q.getResultList();
}
public Employee getEmployee(Integer id){
return em.find(Employee.class, id);
}
public void updateEmployee(Employee e){
Employee emp = getEmployee(e.getId());
emp.setFirstName(e.getFirstName());
emp.setLastName(e.getLastName());
emp.setDepartmentId(e.getDepartmentId());
emp.setEmail(e.getEmail());
emp.setHireDate(e.getHireDate());
emp.setManagerId(e.getManagerId());
emp.setPhone(e.getPhone());
em.persist(emp);
em.flush();
}
@Override
public Employee addEmployee(Employee e) {
em.persist(e);
em.flush();
return e;
}
public void deleteEmployee(Integer id) {
Employee e = em.find(Employee.class, id);
if ( e == null ) {
System.err.println("Error deleteing employee " + id);
}else {
em.remove(e);
em.flush();
}
}
}
的index.xhtml
<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:outputText value="#{employeeSearchController}"></h:outputText>
<f:loadBundle basename="resources.application" var="msg"/>
<head>
<title><h:outputText value="#{msg.welcomeTitle}" /></title>
</head>
<body>
<CENTER><h1><b>Employee Details</b></h1></CENTER>
<h4 style="color:red">${error}</h4>
<h:form>
<fieldset style="width:=300px">
<legend>Find Employee</legend>
<table>
<tr>
<td>Employee ID:</td>
<td><h:inputText id="i1" value="#{empController.empSearchID}" label="Employee Id"></h:inputText></td>
</tr>
</table>
</fieldset>
<h:commandButton value="Search" action="#{empController.Search}" />
</h:form>
<legend>Add Employee</legend>
<h:form>
<table>
<tr>
<td>Employee ID (Key)*:</td>
<td><h:inputText id="i2" value="#{empController.emp.id}" label="EmployeeId"></h:inputText></td>
</tr>
<tr>
<td>First Name *:</td>
<td><h:inputText id="i3" value="#{empController.emp.firstName}" label="FirstName"></h:inputText></td>
</tr>
<tr>
<td>Last Name: *:</td>
<td><h:inputText id="i4" value="#{empController.emp.lastName}" label="LastName"></h:inputText></td>
</tr>
<tr>
<td> Email: </td>
<td><h:inputText id="i5" value="#{empController.emp.email}" label="email"></h:inputText></td>
</tr>
<tr>
<td> Phone: </td>
<td><h:inputText id="i6" value="#{empController.emp.phone}" label="phoneNumber"></h:inputText></td>
</tr>
<tr>
<td>Hire Date(MM/DD/yyyy):</td>
<td><h:inputText id="i7" value="#{empController.emp.hireDate}" label="hireDate"></h:inputText></td>
</tr>
<tr>
<td>Manager Id:</td>
<td><h:inputText id="i8" value="#{empController.emp.managerId}" label="Manager ID"></h:inputText></td>
</tr>
<tr>
<td>Department Id:</td>
<td><h:inputText id="i9" value="#{empController.emp.departmentId}" label="DepartmentID"></h:inputText></td>
</tr>
</table>
<h:commandButton value="Add" immediate="true" action="#{empController.AddEmp}" />
</h:form>
</body>
<h:commandButton value="update Employee" immediate="true" action="#{empController.updateEmp}" />
</html>
NewEmployee.xhtml
<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:loadBundle basename="resources.application" var="msg"/>
<head>
<title><h:outputText value="#{msg.welcomeTitle}" /></title>
</head>
<h:body>
<h:dataTable value="#{empController.list}" var="e" border="2">
<h:column>
<f:facet name="header">
<h:outputText value="First Name"/>
</f:facet>
<h:outputText value="#{e.firstName}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Last Name"/>
</f:facet>
<h:outputText value="#{e.lastName}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Email"/>
</f:facet>
<h:outputText value="#{e.email}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Phone"/>
</f:facet>
<h:outputText value="#{e.phone}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Hire Date"/>
</f:facet>
<h:outputText value="#{e.hireDate}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Manager ID"/>
</f:facet>
<h:outputText value="#{e.managerId}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Department ID"/>
</f:facet>
<h:outputText value="#{e.departmentId}"/>
</h:column>
</h:dataTable>
</h:body>
</html>