5

我知道 managedBean 有问题,但我不知道它是什么

departmentManagedBean.department.faculty.facultyName当我从 jsf 页面尝试时,它会给出一个空指针,如下所示:

javax.el.PropertyNotFoundException: /administrator/department/DepartmentRegistration.xhtml @30,260 value="#{departmentRequestBean.department.faculty.facultyName}": 目标不可达,'null' 返回 null

javax.el.PropertyNotFoundException: /administrator/department/DepartmentRegistration.xhtml @30,260 value="#{departmentRequestBean.department.faculty.facultyName}": Target Unreachable, 'null' returned null
at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:100)
at org.richfaces.component.util.InputUtils.findConverter(InputUtils.java:123)
at org.richfaces.component.util.InputUtils$1.getConverterByProperty(InputUtils.java:56)
at org.richfaces.component.util.InputUtils.getConvertedValue(InputUtils.java:148)
at org.richfaces.component.util.InputUtils.getConvertedValue(InputUtils.java:137)
at org.richfaces.renderkit.InputRendererBase.getConvertedValue(InputRendererBase.java:50)
at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1030)
at javax.faces.component.UIInput.validate(UIInput.java:960)
at javax.faces.component.UIInput.executeValidate(UIInput.java:1233)
at javax.faces.component.UIInput.processValidators(UIInput.java:698)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIForm.processValidators(UIForm.java:253)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1172)
at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:636)

public class Department implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "DepartmentID")
    private Integer departmentID;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "DepartmentName")
    private String departmentName;
    @JoinColumn(name = "FacultyName", referencedColumnName = "FacultyName")
    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    public Faculty faculty;

    public Faculty getFaculty() {
        return faculty;
    }

    public void setFaculty(Faculty faculty) {
        this.faculty = faculty;
    }
    public Department() {

    }

    public Department(Integer departmentID) {
        this.departmentID = departmentID;
    }

    public Department(Integer departmentID, String departmentName) {
        this.departmentID = departmentID;
        this.departmentName = departmentName;
    }

    public Integer getDepartmentID() {
        return departmentID;
    }

    public void setDepartmentID(Integer departmentID) {
        this.departmentID = departmentID;
    }

    public String getDepartmentName() {
        return departmentName;
    }

    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }
}

public class DepartmentSessionBean {
    @PersistenceContext(unitName = "ApplicationPU")
    private EntityManager em;

    public void persist(Department department) {     
        em.persist(department);
    }
}

public class Faculty implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "FacultyID")
    private Integer facultyID;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "FacultyName")
    private String facultyName;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "faculty", fetch = FetchType.EAGER)
    private List<Department> departmentList;

    public Faculty() {
    }

    public Faculty(Integer facultyID) {
        this.facultyID = facultyID;
    }

    public Faculty(Integer facultyID, String facultyName) {
        this.facultyID = facultyID;
        this.facultyName = facultyName;
    }

    public Integer getFacultyID() {
        return facultyID;
    }

    public void setFacultyID(Integer facultyID) {
        this.facultyID = facultyID;
    }

    public String getFacultyName() {
        return facultyName;
    }

    public void setFacultyName(String facultyName) {
        this.facultyName = facultyName;
    }

    public List<Department> getDepartmentList() {
        return departmentList;
    }

    public void setDepartmentList(List<Department> departmentList) {
        this.departmentList = departmentList;
    }
}

这是我需要解决方案的 ManagedBean:

@ViewScoped public class DepartmentRequestBean {
@EJB
private DepartmentSessionBean departmentSessionBean;
private Department department = new Department();
public Department getDepartment() {
    return department;
}

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


public void save(){


        departmentSessionBean.persist(department);

}

public DepartmentRequestBean() {
    this.faculty = new Faculty();
}}
4

2 回答 2

8

value="#{departmentRequestBean.department.faculty.facultyName}": 目标不可达,'null' 返回 null

这基本上意味着以下值之一

  • #{departmentRequestBean.department}
  • #{departmentRequestBean.department.faculty}

null在处理表单提交期间返回。由于您已经明确创建了Department自己,那么它只能表示Faculty仍然是null.

要正确修复它,请更换DepartmentRequestBean

private Department department = new Department();

经过

private Department department;

@PostConstruct
public void init() {
    department = new Department();
    department.setFaculty(new Faculty());
}
于 2012-07-16T13:14:58.173 回答
0

像这样在 backing bean 中初始化你的部门类

public Department getDepartment() {
if(department == null){
department = (Department)super.getInstance(Department.class);
}
return department;
}

如果对象变为空,这将创建一个新的部门对象

于 2014-01-31T09:20:50.163 回答