2014-08-30 75 views
10

我在運行我的JSF頁面時出現以下錯誤。錯誤:javax.el.PropertyNotFoundException:目標不可訪問,'null'返回null

javax.el.PropertyNotFoundException:目標不可達, '空' 返回null ..

警告:/createStaff.xhtml @ 33125 值= 「#{staffBean.staff.firstName}」:目標不可達, 'NULL' 返回null javax.el.PropertyNotFoundException:/createStaff.xhtml @ 33125值= 「#{staffBean.staff.firstName}」:目標不可達, 'NULL' 返回null

我不我不明白爲什麼我會在使用時遇到錯誤值= "#{staffBean.staff.firstName}"。當我使用上面的值= "#{staffBean.userName}"和值= "#{staffBean.passWord}"時沒有問題。

這是我createStaff.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://xmlns.jcp.org/jsf/html" 
     xmlns:f="http://xmlns.jcp.org/jsf/core" 
     xmlns:p="http://primefaces.org/ui"> 
    <h:head> 
     <title>Create Staff</title> 
    </h:head> 
    <h:body> 
     <f:view> 
      <h:form> 
       <p:panel id ="panel" header="Staff Creation"> 
        <p:messages id="msgs" /> 
        <h:panelGrid columns="3" columnClasses="label, value"> 
         <h:outputText value="Username: *" /> 
         <p:inputText id="username" value="#{staffBean.userName}" required="true" label="Username"> 
         </p:inputText> 
         <p:message for="username" /> 

         <h:outputLabel for="pwd1" value="Password 1: *" /> 
         <p:password id="pwd1" value="#{staffBean.passWord}" match="pwd2" label="Password 1" required="true" feedback="true" /> 
         <p:message for="pwd1" /> 

         <h:outputLabel for="pwd2" value="Password 2: *" /> 
         <p:password id="pwd2" value="#{staffBean.passWord}" label="Password 2" required="true" feedback="true" /> 
         <p:message for="pwd2" /> 

         <h:outputText value="First name: *" /> 
         <p:inputText id="firstname" value="#{staffBean.staff.firstName}" required="true" label="Username"> 
         </p:inputText> 
         <p:message for="firstname" /> 

         <h:outputText value="Last name: *" /> 
         <p:inputText id="lastname" value="#{staffBean.staff.lastName}" required="true" label="Username"> 
         </p:inputText> 
         <p:message for="lastname" /> 

         <h:outputText value="Last name: *" /> 
         <p:selectOneRadio id="genderconsole" value="#{staffBean.staff.gender}" required="true"> 
          <f:selectItem itemLabel="Male" itemValue="Male" /> 
          <f:selectItem itemLabel="Female" itemValue="Female" /> 

         </p:selectOneRadio> 
         <p:message for="genderconsole" /> 


         <p:commandButton value="Create Staff" 
             id="ajax" 

             update="panel"> 

         </p:commandButton> 
        </h:panelGrid> 
       </p:panel> 
      </h:form> 
     </f:view> 
    </h:body> 
</html> 

這是我StaffBean.java

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package managedbean; 

import entities.Staff; 
import java.io.IOException; 
import java.io.Serializable; 
import java.util.HashSet; 
import java.util.Set; 
import javax.ejb.EJB; 
import javax.enterprise.context.SessionScoped; 
import javax.faces.FacesException; 
import javax.faces.application.FacesMessage; 
import javax.faces.context.ExternalContext; 
import javax.faces.context.FacesContext; 
import javax.faces.view.ViewScoped; 
import javax.inject.Named; 
import sessionBean.staffSessionBeanLocal; 


@Named(value = "staffBean") 
@SessionScoped 
//@ViewScoped 
public class StaffBean implements Serializable { 

    @EJB 
    private staffSessionBeanLocal staffSession; 
    private String userName; 
    private String passWord; 
    private String loginStatus; 
    private Staff staff; 

........... 
////Code removed 

    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 getLoginStatus() { 
     return loginStatus; 
    } 

    public void setLoginStatus(String loginStatus) { 
     this.loginStatus = loginStatus; 
    } 

    public Staff getStaff() { 
     return staff; 
    } 

    public void setStaff(Staff staff) { 
     this.staff = staff; 
    } 

} 

這是我的員工實體。

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 

package entities; 

import java.io.Serializable; 
import java.util.ArrayList; 
import java.util.List; 
import javax.persistence.CascadeType; 
import javax.persistence.Entity; 
import javax.persistence.FetchType; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.ManyToMany; 


@Entity 
public class Staff extends User implements Serializable { 
    private static final long serialVersionUID = 1L; 
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Long id; 
    private String imageURL; 
    @ManyToMany(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER) 
    private List<Roles> roles = new ArrayList<Roles>(); 

    @Override 
    public Long getId() { 
     return id; 
    } 

    @Override 
    public void setId(Long id) { 
     this.id = id; 
    } 

    @Override 
    public int hashCode() { 
     int hash = 0; 
     hash += (id != null ? id.hashCode() : 0); 
     return hash; 
    } 

    @Override 
    public boolean equals(Object object) { 
     // TODO: Warning - this method won't work in the case the id fields are not set 
     if (!(object instanceof Staff)) { 
      return false; 
     } 
     Staff other = (Staff) object; 
     if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public String toString() { 
     return "entities.Staff[ id=" + id + " ]"; 
    } 

    public String getImageURL() { 
     return imageURL; 
    } 

    public void setImageURL(String imageURL) { 
     this.imageURL = imageURL; 
    } 

    public List<Roles> getRoles() { 
     return roles; 
    } 

    public void setRoles(List<Roles> roles) { 
     this.roles = roles; 
    } 

} 

這是我的用戶類,Staff類擴展自。

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 

package entities; 

import java.io.Serializable; 
import java.sql.Timestamp; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.MappedSuperclass; 


@MappedSuperclass 
public class User implements Serializable { 
    private static final long serialVersionUID = 1L; 
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Long id; 
    private String password; 
    private Timestamp joinDate; 
    private String userType; 
    private String gender; 
    private String email; 
    private String contactNo; 
    private String firstName; 
    private String lastName; 
    private Timestamp dOB; 
    private String address; 
    private String accountStatus; 
    private int numOfFailLogin; 
    private String maritalStatus; 
    private String activationCode; 


    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    @Override 
    public int hashCode() { 
     int hash = 0; 
     hash += (id != null ? id.hashCode() : 0); 
     return hash; 
    } 

    @Override 
    public boolean equals(Object object) { 
     // TODO: Warning - this method won't work in the case the id fields are not set 
     if (!(object instanceof User)) { 
      return false; 
     } 
     User other = (User) object; 
     if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public String toString() { 
     return "entities.User[ id=" + id + " ]"; 
    } 


    public String getPassword() { 
     return password; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 

    public Timestamp getJoinDate() { 
     return joinDate; 
    } 

    public void setJoinDate(Timestamp joinDate) { 
     this.joinDate = joinDate; 
    } 

    public String getUserType() { 
     return userType; 
    } 

    public void setUserType(String userType) { 
     this.userType = userType; 
    } 

    public String getGender() { 
     return gender; 
    } 

    public void setGender(String gender) { 
     this.gender = gender; 
    } 

    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 

    public String getContactNo() { 
     return contactNo; 
    } 

    public void setContactNo(String contactNo) { 
     this.contactNo = contactNo; 
    } 

    public String getFirstName() { 
     return firstName; 
    } 

    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 

    public String getLastName() { 
     return lastName; 
    } 

    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 

    public Timestamp getdOB() { 
     return dOB; 
    } 

    public void setdOB(Timestamp dOB) { 
     this.dOB = dOB; 
    } 

    public String getAddress() { 
     return address; 
    } 

    public void setAddress(String address) { 
     this.address = address; 
    } 

    public String getAccountStatus() { 
     return accountStatus; 
    } 

    public void setAccountStatus(String accountStatus) { 
     this.accountStatus = accountStatus; 
    } 

    public String getMaritalStatus() { 
     return maritalStatus; 
    } 

    public void setMaritalStatus(String maritalStatus) { 
     this.maritalStatus = maritalStatus; 
    } 

    public int getNumOfFailLogin() { 
     return numOfFailLogin; 
    } 

    public void setNumOfFailLogin(int numOfFailLogin) { 
     this.numOfFailLogin = numOfFailLogin; 
    } 

    public String getActivationCode() { 
     return activationCode; 
    } 

    public void setActivationCode(String activationCode) { 
     this.activationCode = activationCode; 
    } 

} 

回答

20

你有沒有財產firstName在實體staff

UPDATE:

看起來你staff對象爲空加:

@PostConstruct 
public void init() { 
    staff = new Stuff(); 
} 
+0

您好。我的用戶實體中有firstName屬性。員工擴展用戶類。任何關於問題出在哪裏的想法? – 2014-08-30 16:59:00

+0

@勞倫斯·旺已經更新了我的答案。 – Jens 2014-08-30 17:12:24

+0

謝謝!我的代碼現在正在工作。 – 2014-08-30 17:18:22

5

的錯誤提示,當「名字「正在被訪問,無法訪問。所以「工作人員」還沒有建成。

向託管bean添加一個方法,這將解決問題。

@PostConstruct 
public void init() { 
    staff= new Staff(); 
} 

爲了更好地理解爲什麼你應該這樣做的,並沒有

工作人員的工作人員=新員工();

JSF - what is the difference between @PostConstruct and direct method call from constructor?

+0

感謝您的鏈接。 立即閱讀。 – 2014-08-30 17:15:46

相關問題