Closed. This question needs debugging details。它当前不接受答案。












想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。

5年前关闭。





我添加了preconstruct注释,但是每当我尝试提交员工时,我都会遇到相同的错误。在这一点上,我不确定该怎么办。任何帮助将不胜感激谢谢

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.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import ejb605.assignment2.com.EmployeeManager;
import entity.Employee;

@ManagedBean
@SessionScoped
public class EmpController implements Serializable{



    @EJB
    EmployeeManager em;

    private String empByID;
    private String firstName;
    private String lastName;
    private String email;
    private String phoneNumber;
    private Date hireDate;
    private int managerID;
    private int departmentID;


    private List<Employee> list = new ArrayList<>();
    private Employee emp;
     @PostConstruct
        public void init() {
            this.emp = new Employee();
        }

    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;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public Date getHireDate() {
        return hireDate;
    }

    public void setHireDate(Date hireDate) {
        this.hireDate = hireDate;
    }

    public int getManagerID() {
        return managerID;
    }

    public void setManagerID(int managerID) {
        this.managerID = managerID;
    }

    public int getDepartmentID() {
        return departmentID;
    }

    public void setDepartmentID(int departmentID) {
        this.departmentID = departmentID;
    }


    public List<Employee> getList() {
        list = em.getAllEmployees();
        return list;
    }

    public String getEmpByID() {
        return empByID;
    }

    public void setEmpByID(String empByID) {
        this.empByID = empByID;
    }

    public Employee getEmp() {
        return emp;
    }


    public void setEmp(Employee emp) {
        this.emp = emp;
    }


    public String Search(){
        emp = null;
        this.setEmp(em.getEmployee(Integer.parseInt(this.empByID)));
        return "EmployeeSearchDetails";
    }

    public String AddEmployee(){
        this.setEmp(em.addEmployee(emp));
        list = em.getAllEmployees();
        return "NewEmployee";
    }

}


Employee.java

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);
    }

    @Override
    public Employee addEmployee(Employee e) {
        em.persist(e);
        return e;
    }

}


<!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.empByID}" label="Employee Id"></h:inputText></td>
            </tr>
        </table>
    </fieldset>
        <h:commandButton value="Search" action="#{EmpController.Search}" />
      </h:form>
    <legend>Employee Information</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" action="#{EmpController.AddEmployee}" />
              </h:form>

</body>
</html>

最佳答案

创建ManagedBean时,首字母默认为小写。

代替#{EmpController ...},它应该是#{empController ...}

09-09 22:55