也许我不知道怎么问我的问题,这就是为什么我找不到答案。所以,我们走了。
我正在玩/测试一些东西,当我使用List<>时我感到难过在非托管bean中(或为此管理)。
我有一个名为PayrollSummaryWizard.java的java托管bean,用于primeFaces向导。在这个托管bean中,我有一个java类,它包含2个列表。一个持有一种佣金,另一个持有另一个。我将这些佣金设置为他们自己的java类。
package com.testing.xx.payrollsummary;
import java.io.Serializable;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import org.primefaces.event.FlowEvent;
@ViewScoped
@Named
public class PayrollSummaryWizard implements Serializable {
private PayrollSummary payrollSummary = new PayrollSummary();
public PayrollSummary getPayrollSummary() {
return payrollSummary;
}
public void setPayrollSummary(PayrollSummary payrollSummary) {
this.payrollSummary = payrollSummary;
}
private String onFlowProcess(FlowEvent event)
{
return event.getNewStep();
}
private void save()
{
}
}
package com.testing.xx.payrollsummary;
import java.io.Serializable;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class PayrollSummary implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private List<CountCommission> countCommissions;
public List<CountCommission> getCountCommissions() {
return carCommissions;
}
public void setCountCommissions(List<CountCommission> countCommissions) {
this.countCommissions = countCommissions;
}
public List<SalesCommission> getSalesCommissions() {
return salesCommissions;
}
public void setSalesCommissions(List<SalesCommission> salesCommissions) {
this.salesCommissions = salesCommissions;
}
public Long getManagersEmployeeNumber() {
return managersEmployeeNumber;
}
public void setManagersEmployeeNumber(Long managersEmployeeNumber) {
this.managersEmployeeNumber = managersEmployeeNumber;
}
public String getStore() {
return store;
}
public void setStore(String store) {
this.store = store;
}
private List<SalesCommission> salesCommissions;
private Long managersEmployeeNumber;
private String store;
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 PayrollSummary)) {
return false;
}
PayrollSummary other = (PayrollSummary) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
}
package com.testing.xx.payrollsummary;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class SalesCommission implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private Double salesAmount;
public Double getSalesAmount() {
return salesAmount;
}
public void setSalesAmount(Double salesAmount) {
this.salesAmount = salesAmount;
}
public Long getEmployeeNumber() {
return employeeNumber;
}
public void setEmployeeNumber(Long employeeNumber) {
this.employeeNumber = employeeNumber;
}
private Long employeeNumber;
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 SalesCommission)) {
return false;
}
SalesCommission other = (SalesCommission) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
}
问题出现在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:p="http://primefaces.org/ui">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<p:panelGrid>
<p:wizard flowListener="#{payrollSummaryWizard.onFlowProcess}">
<p:tab id="personal" title="Personal">
<p:panel header="Personal Details">
<p:messages />
<h:panelGrid columns="2" columnClasses="label, value">
<h:outputLabel for="managersEmployeeNumber" value="Manager's Employee Number:" />
<p:inputText id="managersEmployeeNumber" value="#{payrollSummaryWizard.payrollSummary.managersEmployeeNumber}" required="true" label="Manager's Employee Number"/>
<h:outputLabel for="store" value="Store:" />
<p:inputText id="store" value="#{payrollSummaryWizard.payrollSummary.store}" required="true" label="Store"/>
</h:panelGrid>
</p:panel>
</p:tab>
<p:tab id="personal" title="Personal">
<p:panel header="Personal Details">
<p:messages />
<h:panelGrid columns="2" columnClasses="label, value">
<h:outputLabel for="salesCommission_employeeNumber" value="Employee Number:" />
<p:inputText id="salesCommission_employeeNumber" value="#{payrollSummaryWizard.payrollSummary.salesCommissions.??}" required="true" label="Employee Number"/>
</h:panelGrid>
</p:panel>
</p:tab>
</p:wizard>
</p:panelGrid>
</h:form>
</h:body>
</html>
如何访问该列表?
value="#{payrollSummaryWizard.payrollSummary.salesCommissions.??}"
在哪里??,我正在尝试做员工编号。尝试使用数组类型,但它看起来不会起作用吗?
编辑:我不确定这是否重要,但我之所以这样想是因为,在页面上有一个“添加”按钮会很好,动态新字段会出现在这里,它们可以是映射回列表,如果这是有道理的。通过这种方式,我可以在本周的1个表单上添加更多员工销售佣金。我在这里发现了两个类似的问题,但他们并不是这样。这是一个因为我丢失了与另一个的链接:/
JSF - List of objects in a Managed Bean and memory management question
编辑:我不认为这应该被认为是一个重复的问题,因为,其他相似的问题提供了一个对此无效的答案。你得到的只是空引用。我已经尝试使用[0] .employeeNumber,因为我的想法是列表可以像数组一样访问,这里,即使使用@PostConstruct进行初始化,或者没有,它仍然会抛出空指针引用。其次,实际列表不存储在@ManagedBean中,它位于托管bean引用的POJO中。
对不起我是如此散乱,就像我说的,我不知道如何提出这个问题。
所以,这是我目前尝试过的代码:
package com.testing.xx.payrollsummary;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import org.primefaces.event.FlowEvent;
/**
*
* @author cfranklin
*/
@SessionScoped
@ManagedBean
public class PayrollSummaryWizard implements Serializable {
private PayrollSummary payrollSummary = new PayrollSummary();
@PostConstruct
public void init() {
payrollSummary = new PayrollSummary();
}
public PayrollSummary getPayrollSummary() {
return payrollSummary;
}
public void setPayrollSummary(PayrollSummary payrollSummary) {
this.payrollSummary = payrollSummary;
}
public String onFlowProcess(FlowEvent event) {
return event.getNewStep();
}
private void save() {
}
}
在这个@ManagedBean中,我尝试了所有不同的范围,没有工作。第二,在过去,当我想用@PostConstruct填充数组传递给dropdwon时,由于某种原因,范围很重要,这就是为什么我现在尝试了所有这些。
@ManagedBean引用的POJO是这样的:
package com.testing.xx.payrollsummary;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity
public class PayrollSummary implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToMany
private List<CountCommission> countCommissions = new ArrayList();
@OneToMany
private List<SalesCommission> salesCommissions = new ArrayList();
private Long managersEmployeeNumber;
private String store;
private static final long serialVersionUID = 1L;
public void PayrollSummary(){
salesCommissions = new ArrayList();
carCommissions = new ArrayList();
}
public List<CountCommission> getCountCommissions() {
return countCommissions;
}
public void setCountCommissions(List<CountCommission> countCommissions) {
this.countCommissions = countCommissions;
}
public List<SalesCommission> getSalesCommissions() {
return salesCommissions;
}
public void setSalesCommissions(List<SalesCommission> salesCommissions) {
this.salesCommissions = salesCommissions;
}
public Long getManagersEmployeeNumber() {
return managersEmployeeNumber;
}
public void setManagersEmployeeNumber(Long managersEmployeeNumber) {
this.managersEmployeeNumber = managersEmployeeNumber;
}
public String getStore() {
return store;
}
public void setStore(String store) {
this.store = store;
}
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 PayrollSummary)) {
return false;
}
PayrollSummary other = (PayrollSummary) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
}
这是列表
中的一个对象package com.testing.xx.payrollsummary;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class SalesCommission implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private Long employeeNumber;
private Double salesAmount;
@OneToOne
private PayrollSummary payrollSummary;
public PayrollSummary getPayrollSummary() {
return payrollSummary;
}
public void setPayrollSummary(PayrollSummary payrollSummary) {
this.payrollSummary = payrollSummary;
}
public Double getSalesAmount() {
return salesAmount;
}
public void setSalesAmount(Double salesAmount) {
this.salesAmount = salesAmount;
}
public Long getEmployeeNumber() {
return employeeNumber;
}
public void setEmployeeNumber(Long employeeNumber) {
this.employeeNumber = employeeNumber;
}
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 SalesCommission)) {
return false;
}
SalesCommission other = (SalesCommission) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
}
我只是保持这部分简短易读:
<p:inputText id="saleCommission_employeeNumber_0" value="#{payrollSummaryWizard.payrollSummary.saleCommissions[0].employeeNumber}" required="true" label="Employee Number"/>
输出
javax.el.PropertyNotFoundException: /index.xhtml @30,203 value="#{payrollSummaryWizard.payrollSummary.saleCommissions[0].employeeNumber}": Target Unreachable, 'null' returned null
因此,我不认为Get specific element in a list or array using EL适用于这种情况。或许它确实如此,我没有正确初始化?我也尝试过几种不同的方式。