Jsf 租用日期(年月日): 经理Id: 部门编号:

Jsf 租用日期(年月日): 经理Id: 部门编号:,jsf,jpa,ejb,Jsf,Jpa,Ejb,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"

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>


为什么人们总是不必要地坚持使用详细会话范围的托管bean?在上面描述的这一集中,您真的需要会话范围的托管bean吗?那是大约十年前的JSF1.x时代。从那时起,情况发生了显著的变化。不,它与上述问题无关——它完全脱离主题。当我删除immediate=true时,它不会重定向到JSF页面。我想这就是问题所在。你得到了什么?“不重定向”不是很有用。1) 我会在托管bean中使用单个属性来为员工赋值,而不是实体
employee
本身的实例。2) 您使用的是普通的HTML
标记,而不是标准的JSF
标记。因此,在特定时间加载JavaScript时会遇到问题。3) 您将
错误放置在
标记外。4) 另外,
标签(反过来,它的大写字母令人恼火)很久以前就被弃用了。你现在甚至不需要再记住它了。你也在使用普通的HTML
标记,而不是使用标准的JSF
标记。谢谢你,我会把它们修好的。在index.xhtml表单中搜索员工时,我从托管bean中获得正确的结果。但是,当我尝试添加员工时,我会被重定向,但表单中的值为null。我想知道这是否可以归因于会议的范围?
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();
        }
    }

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