0

大家好,我是 jsp 的新手,这是我学习托管 bean、导航规则和其他东西的第一个项目,但我看不出这里有什么问题。我所有的代码都是那些。请帮助我,因为我要疯了才能找到这个。错误是 /pages/login.jsp(31,2) '#{employee.username}' 目标无法访问,标识符 'employee' 解析为 null

雇员.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;
       }    

}

用户数据.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;
     }
}

主页.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>

登录.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>

面孔-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>

索引.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>

错误信息

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)
4

1 回答 1

0

正如 BalusC 所说,这在很久以前就被弃用了。除了他提到的资源,我认为你应该在他自己的博客上搜索一下,在那里你会找到学习 JSF 所需的一切。
至于您真正的问题,请注意,在您的login.jsp页面中,您正在关闭datatable定义employeevar 的位置:

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

就在评论部分之前。然后你打电话

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

employee无法识别 var是很正常的。

于 2013-06-21T12:26:19.937 回答