maybe I do not know how to ask my question, and that is why I cannot find an answer. So, here we go.
I am just playing around/testing some stuff and I am stumped when using a List<> in a non-managed bean (or managed for that matter).
I have a java managed bean called PayrollSummaryWizard.java, for use with the primeFaces wizard. Inside this managed bean, I have a java class, which contains 2 lists. One holds one type of commission, and the other list holds another. I set these commissions up as their own java classes.
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;
}
}
The problem comes in, inside the 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>
How do I access that list?
value="#{payrollSummaryWizard.payrollSummary.salesCommissions.??}"
where ??, I am trying to do the employee number. Was trying with an array type, but it doesnt look like that would work?
EDIT: I am not sure if it matters, but the reason I was thinking like this is because, it would be nice to have an "Add" button on the page, where dynamic new fields would show up and they can be mapped back to the list, if that makes sense. This way, I could add more employee sales commissions on 1 form for the week.
I found two similar questions on here, but they weren't quite this. Here is one because I lost the link to the other one :/
JSF - List of objects in a Managed Bean and memory management question
EDIT: I do not think this should be considered a duplicate question because, the other questions that are similar provide an answer that does not work for this. All you get are null references. I have tried using the [0].employeeNumber because, my thinking is that the list can be accessed like an array, only, here, even with an @PostConstruct to initialize, or without, it still throws a null pointer reference. Second, the actual list is not stored within the @ManagedBean, it's in a POJO being referenced by a managed bean.
Sorry I am so scatterbrained, like I said, I am not sure how to ask this question.
So, here is the code that I have tried currently:
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() {
}
}
In this @ManagedBean, I have tried all of the different scopes, none work. Second, in the past, when I wanted to fill an array with the @PostConstruct to pass to a dropdwon, for some reason, the scope mattered, which is why I tried all of them now.
the POJO that the @ManagedBean is referencing is like so:
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;
}
}
And here is one of the objects that belongs in the list
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;
}
}
I am just keeping this part short and simple for readability:
<p:inputText id="saleCommission_employeeNumber_0" value="#{payrollSummaryWizard.payrollSummary.saleCommissions[0].employeeNumber}" required="true" label="Employee Number"/>
outputs
javax.el.PropertyNotFoundException: /index.xhtml @30,203 value="#{payrollSummaryWizard.payrollSummary.saleCommissions[0].employeeNumber}": Target Unreachable, 'null' returned null
Hence, I do not think Get specific element in a list or array using EL applies in this situation. Or maybe it does and I am not initializing properly? I've also tried doing that a few different ways.