1

I'm new to JSF.I followed a tutorial of JSF step by step but I'm unable to get the data from ManagedBean to xhtml page when I run it.Below are each class I have created.

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>

Can someone help me out to figure out what is the exact issue and how to overcome it.Thanks in advance.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Bawge Chandu
  • 15
  • 2
  • 8

1 Answers1

0

Because you have userData.dept, expression language will look for getDept()/setDept() methods which u don't have. which will throw an error. You need use the proper naming.

Change

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

To

<h:inputText size="20" value="#{userData.department}" />
fareed
  • 3,034
  • 6
  • 37
  • 65