0
votes

hi everyone Im new at jsp and this is my first project I learned managed beans, navigation rules and stuff and I cant see what the problem is in here. all my codes are those. Please help me because Im going crazy trying to find this. ERROR is /pages/login.jsp(31,2) '#{employee.username}' Target Unreachable, identifier 'employee' resolved to null

Employee.java

package com;

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

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

       public String getUsername() {
          return username;
       }
       public void setUsername(String username) {
          this.username = username;
       }
       public String getPassword() {
          return password;
       }
       public void setPassword(String password) {
          this.password = password;
       }
       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;
       }    

}

UserData.java

package com;

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

public class UserData implements Serializable {

private static final long serialVersionUID = 1L;

   private String username;
   private String password;
   private String name;
   private String department;
   private int age;
   private double salary;

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


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

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

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

   public String editEmployee(Employee employee){
      employee.setCanEdit(true);
      return null;
   }
   public String saveEmployees(){
      for (Employee employee : employees){
         employee.setCanEdit(false);
      }     
      return null;
   }
       public String getUsername() {
          return username;
       }
       public void setUsername(String username) {
          this.username = username;
       }
       public String getPassword() {
          return password;
       }
       public void setPassword(String password) {
          this.password = password;
       }
   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 String loginAction(){
         String action = null;

         if (username.equalsIgnoreCase("admin") && password.equalsIgnoreCase("a"))
             action = "loginAdmin";
         else
             action = "loginUser";

         return action;
     }
}

home.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style>
.employeeTable{   
   border-collapse:collapse;
   border:1px solid #000000;
}

.employeeTableHeader{
   text-align:center;
   background:none repeat scroll 0 0 #B5B5B5;
   border-bottom:1px solid #000000;  
   padding:2px;
}

.employeeTableOddRow{
   text-align:center;
   background:none repeat scroll 0 0 #FFFFFFF;  
}

.employeeTableEvenRow{
   text-align:center;
   background:none repeat scroll 0 0 #D3D3D3;
}</style>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      <title>DataTable</title>      
</head>
<body><f:view>
   <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>                   
            <h:inputText id="name" value="#{employee.name}"></h:inputText>
         </h:column>
         <h:column>
            <f:facet name="header">Department</f:facet>
            <h:inputText id="department" value="#{employee.department}"></h:inputText>
         </h:column>
         <h:column>
            <f:facet name="header">Age</f:facet>
            <h:inputText id="age" value="#{employee.age}"></h:inputText>
         </h:column>
         <h:column>
            <f:facet name="header">Salary</f:facet>
            <h:inputText id="salary" value="#{employee.salary}"></h:inputText>
         </h:column>
         <h:column>

               <h:commandButton value="Delete" 
                  action="#{userData.deleteEmployee}" >                
                  <f:setPropertyActionListener 
                     target="#{userData.employees}" value="#{employee}" />
               </h:commandButton>

         </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.department}" /></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></f:view>
</body>
</html>

login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<f:view>
    <b><h:outputText value="USER Login"> </h:outputText></b>
    <p>
    <h:messages style="color: blue"/></p>
 <h:form>
    <h:panelGrid  columns="2">
    <h:dataTable value="#{userData.employees}" var="employee"></h:dataTable>
    <%-- 
    <h:dataTable value="#{userData.employees}" var="employee"></h:dataTable>
    <h:column>                  
            <h:outputText id="header" value="username"></h:outputText>                  
            <h:inputText id="username" value="#{employee.username}"></h:inputText>
     <br><br>                   
            <h:outputText id="header" value="password"></h:outputText>                  
            <h:inputText id="password" value="#{employee.password}"></h:inputText>
     </h:column>
    --%>
    <h:column>
    <h:outputText value="username :"></h:outputText>
    <h:inputText id="username"  value="#{employee.username}">
    </h:inputText>
 <br>
    <h:outputText value="password :"></h:outputText>
    <h:inputText id="password" value="#{employee.password}" >
    </h:inputText>
    </h:column>
    </h:panelGrid>
    <h:commandButton value="Login" action="#{userData.loginAction}"></h:commandButton><br/>
    <h:outputLink value="pages/sign.jsf"><h:outputText value="Go for Sign up."/></h:outputLink>
 </h:form>
</f:view>
</body>
</html>

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xi="http://www.w3.org/2001/XInclude"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">
 <managed-bean>
  <managed-bean-name>userData</managed-bean-name>
  <managed-bean-class>com.UserData</managed-bean-class>
  <managed-bean-scope>session</managed-bean-scope>
 </managed-bean>
 <navigation-rule>
  <from-view-id>/pages/login.jsp</from-view-id>
  <navigation-case>
   <from-outcome>loginAdmin</from-outcome>
   <to-view-id>/pages/home.jsp</to-view-id>
  </navigation-case>
  <navigation-case>
   <from-outcome>loginUser</from-outcome>
   <to-view-id>/pages/welcome.jsp</to-view-id>
  </navigation-case>
 </navigation-rule>
</faces-config>

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<jsp:forward page="/pages/login.jsf" />
</body>
</html>

ERROR Message

11:45:47,704 WARN  [lifecycle]   executePhase(PROCESS_VALIDATIONS 3,com.sun.faces.context.FacesContextImpl@23ccb9) threw exception
javax.faces.FacesException: /pages/login.jsp(31,2) '#{employee.username}' Target Unreachable, identifier 'employee' resolved to null
    at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:108)
    at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:244)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
    at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.apache.jasper.el.JspPropertyNotFoundException: /pages/login.jsp(31,2) '#{employee.username}' Target Unreachable, identifier 'employee' resolved to null
    at org.apache.jasper.el.JspValueExpression.getType(JspValueExpression.java:61)
    at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:81)
    at javax.faces.component.UIInput.getConvertedValue(UIInput.java:934)
    at javax.faces.component.UIInput.validate(UIInput.java:860)
    at javax.faces.component.UIInput.executeValidate(UIInput.java:1065)
    at javax.faces.component.UIInput.processValidators(UIInput.java:666)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1033)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1033)
    at javax.faces.component.UIForm.processValidators(UIForm.java:229)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1033)
    at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:662)
    at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:100)
    ... 21 more
11:45:47,704 ERROR [[Faces Servlet]]   Servlet.service() for servlet Faces Servlet threw exception
org.apache.jasper.el.JspPropertyNotFoundException: /pages/login.jsp(31,2) '#{employee.username}' Target Unreachable, identifier 'employee' resolved to null
    at org.apache.jasper.el.JspValueExpression.getType(JspValueExpression.java:61)
    at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:81)
    at javax.faces.component.UIInput.getConvertedValue(UIInput.java:934)
    at javax.faces.component.UIInput.validate(UIInput.java:860)
    at javax.faces.component.UIInput.executeValidate(UIInput.java:1065)
    at javax.faces.component.UIInput.processValidators(UIInput.java:666)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1033)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1033)
    at javax.faces.component.UIForm.processValidators(UIForm.java:229)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1033)
    at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:662)
    at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:100)
    at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:244)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
    at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
    at java.lang.Thread.run(Unknown Source)
1
Throw away the resource you're currently using to learn JSF. The code posted so far is legacy JSF 1.x with JSP which was almost 4 years ago deprecated and replaced by JSF 2.x with Facelets. Look specifically for resources which explicitly state "JSF 2.0" or "JSF 2.x". At the bottom of our JSF wiki page you can find some links to sane resources (you can get to our JSF wiki page by hovering the [jsf] tag which you placed below the question until a black box shows up and then clicking therein the info link).BalusC

1 Answers

0
votes

As BalusC said, this is since long time ago deprecated. And in addition to the resources he mentioned, I think you should google his own blog where you will find everything you need to learn JSF.
As for your real problem, notice that in your login.jsp page you are closing the datatable where you defined the employee var:

<h:dataTable value="#{userData.employees}" var="employee"></h:dataTable>

right before the commented part. And then you are calling

<h:inputText id="username"  value="#{employee.username}">

This is very normal that the employee var will not be recognised.