无法从JSF中的ManagedBean获取数据

时间:2016-12-12 06:19:29

标签: jsf

我是JSF的新手。我一步一步地按照JSF的教程进行操作,但是当我运行它时,我无法将数据从ManagedBean获取到xhtml页面。下面是我创建的每个类。

UserData.java

package com.practise.test;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "userData", eager = true)
@SessionScoped
public class UserData implements Serializable {

private static final long serialVersionUID = 1L;

   private String name;
   private String dept;
   private int age;
   private double salary;

   private static final ArrayList<Employee> employees
      = new ArrayList<Employee>(Arrays.asList(
      new Employee("John", "Marketing", 30,2000.00),
      new Employee("Robert", "Marketing", 35,3000.00),
      new Employee("Mark", "Sales", 25,2500.00),
      new Employee("Chris", "Marketing", 33,2500.00),
      new Employee("Peter", "Customer Care", 20,1500.00)
   ));  


   public ArrayList<Employee> getEmployees() {
      return employees;
   }

   public String addEmployee() {         
      Employee employee = new Employee(name,dept,age,salary);
      employees.add(employee);
      return null;
   }

   public String deleteEmployee(Employee employee) {
      employees.remove(employee);       
      return null;
   }

   public String editEmployee(Employee employee){
      employee.setCanEdit(true);
      return null;
   }

   public String saveEmployees(){
      //set "canEdit" of all employees to false 
      for (Employee employee : employees){
         employee.setCanEdit(false);
      }     
      return null;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public String getDepartment() {
      return dept;
   }

   public void setDepartment(String department) {
      this.dept = department;
   }

   public int getAge() {
      return age;
   }

   public void setAge(int age) {
      this.age = age;
   }

   public double getSalary() {
      return salary;
   }

   public void setSalary(double salary) {
      this.salary = salary;
   }
}

Employee.java

package com.practise.test;

public class Employee {
   private String name;
   private String department;
   private int age;
   private double salary;
   private boolean canEdit;

   public Employee (String name,String department,int age,double salary){
      this.name = name;
      this.department = department;
      this.age = age;
      this.salary = salary;
      canEdit = false;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public String getDepartment() {
      return department;
   }

   public void setDepartment(String department) {
      this.department = department;
   }

   public int getAge() {
      return age;
   }

   public void setAge(int age) {
      this.age = age;
   }

   public double getSalary() {
      return salary;
   }

   public void setSalary(double salary) {
      this.salary = salary;
   }

   public boolean isCanEdit() {
      return canEdit;
   }

   public void setCanEdit(boolean canEdit) {
      this.canEdit = canEdit;
   }    
}

home.xhtml

<?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://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
   <h:head>
      <title>JSF tutorial</title>       
      <h:outputStylesheet library="css" name="styles.css"  />   
   </h:head>
   <h:body> 
   <h2>DataTable Example</h2>
   <h:form>
      <h:dataTable value="#{userData.employees}" var="employee"
         styleClass="employeeTable"
         headerClass="employeeTableHeader"
         rowClasses="employeeTableOddRow,employeeTableEvenRow">
         <h:column>                 
            <f:facet name="header">Name</f:facet>                   
            #{employee.name}
         </h:column>
         <h:column>
            <f:facet name="header">Department</f:facet>
            #{employee.department}
         </h:column>
         <h:column>
            <f:facet name="header">Age</f:facet>
            #{employee.age}
         </h:column>
         <h:column>
            <f:facet name="header">Salary</f:facet>
            #{employee.salary}
         </h:column>
      </h:dataTable>
      <h3>Add Employee</h3>
      <hr/>
      <table>
      <tr>
            <td>Name :</td>
            <td><h:inputText size="10" value="#{userData.name}" /></td>
      </tr>
      <tr>
            <td>Department :</td>
            <td><h:inputText size="20" value="#{userData.dept}" /></td>
      </tr>
      <tr>
            <td>Age :</td>
            <td><h:inputText size="5" value="#{userData.age}" /></td>
      </tr>
      <tr>
            <td>Salary :</td>
            <td><h:inputText size="5" value="#{userData.salary}" /></td>
      </tr>
      <tr>
            <td> </td>
            <td><h:commandButton value="Add Employee" 
               action="#{userData.addEmployee}" /></td>
      </tr>
      </table>
   </h:form>
   </h:body>
</html>

有人可以帮助我弄清楚究竟是什么问题以及如何克服它。谢谢。

1 个答案:

答案 0 :(得分:0)

因为您有userData.dept,所以表达式语言会查找您没有的getDept() / setDept()方法。这会引发错误。您需要使用正确的命名。

更改

<h:inputText size="20" value="#{userData.dept}" />

<h:inputText size="20" value="#{userData.department}" />