Reputation: 1
I want to get some information about contacts and display them in a table. I used two managed beans named by PersonalContact
and BusinessContact
and one abstract class named by Contact
including firstName
, lastName
, ID
, email
and mphone
attributes which derives from.Also, one generic class to add beans to an arraylist. I put here just BusinessContact
as a bean, business as XHTML and tableBusiness
as a table and generic class AddressBook
, files of personal is parallel of them.
Inputs getting from screen are assigned to attributes of beans but when the beans go to Arraylist
to be added, its content becomes null. Why is that? Is that about scopes?
Here is my bean:
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean
@ViewScoped
public class BusinessContact extends Contact{
private String companyName,workPhone;
public BusinessContact() {}
public BusinessContact(String companyName, String workPhone, String ID, String firstName, String lastName, String email, String mphone) {
super(ID, firstName, lastName, email, mphone);
this.companyName = companyName;
this.workPhone = workPhone;
}
/**
* Creates a new instance of BusinessContact
*/
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public String getWorkPhone() {
return workPhone;
}
public void setWorkPhone(String workPhone) {
this.workPhone = workPhone;
}
}
Here is my business.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:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
<title>Contact</title>
</h:head>
<h:body>
<h:form>
<h:panelGrid columns="2">
First Name:
<h:inputText id="firstName" required="true"
requiredMessage="Can not be blank" value="#{businessContact.firstName}">
</h:inputText>
ID:
<h:inputText id="id" required="true"
requiredMessage="Can not be blank" value="#{businessContact.ID}">
</h:inputText>
Last Name:
<h:inputText id="lastName" required="true"
requiredMessage="Can not be blank" value="#{businessContact.lastName}">
</h:inputText>
E-mail:
<h:inputText id="email" required="true"
requiredMessage="Can not be blank" value="#{businessContact.email}">
</h:inputText>
Mobile Phone:
<h:inputText id="mphone" required="true"
requiredMessage="Can not be blank" value="#{businessContact.mphone}">
</h:inputText>
Company Name:
<h:inputText id="companyName" required="true"
requiredMessage="Can not be blank" value="#{businessContact.companyName}">
</h:inputText>
Work Phone:
<h:inputText id="workPhone" required="true"
requiredMessage="Can not be blank" value="#{businessContact.workPhone}">
</h:inputText>
</h:panelGrid>
<h:commandButton value="Add" id="add" action="#{addressBook.add(BusinessContact)}"></h:commandButton>
<h:commandButton value="Delete" id="delete" action="#{addressBook.remove(BusinessContact)}"></h:commandButton>
<h:outputLink id="t" value="tableBusiness.xhtml"> Click see the table</h:outputLink>
<ui:debug hotkey="k" rendered="true"/>
</h:form>
</h:body>
This is my generic class for putting beans to an arraylist:
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class AddressBook <T extends Contact> {
private ArrayList<T> list = new ArrayList<T>();
T clas;
public ArrayList<T> getList() {
return list;
}
public void setList(ArrayList<T> list) {
this.list = list;
}
public void add(T obj) {
this.clas=obj;
//System.out.println(obj.getFirstName()+" ");
list.add(obj);
}
public void remove(T obj)
{
list.remove(obj);
}
/**
* Creates a new instance of AddressBook
*/
public AddressBook() {}
}
And lastly, this xhmtl for displaying arraylist in table:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
</h:head>
<h:body>
<h:form>
"#{businessContact.firstName}"
<h:dataTable value="#{addressBook.list}" var="BusinessContact">
<h:column>
<f:facet name="header">
First Name
</f:facet>
<h:outputText value="#{businessContact.firstName}">
</h:outputText>
</h:column>
<h:column>
<f:facet name="header">
Last Name
</f:facet>
#{businessContact.lastName}
</h:column>
<h:column>
<f:facet name="header">
Mobile Phone
</f:facet>
#{businessContact.mphone}
</h:column>
<h:column>
<f:facet name="header">
E-mail
</f:facet>
#{businessContact.email}
</h:column>
<h:column>
<f:facet name="header">
Company Name
</f:facet>
#{businessContact.companyName}
</h:column>
<h:column>
<f:facet name="header">
Work Phone
</f:facet>
#{businessContact.workPhone}
</h:column>
</h:dataTable>
</h:form>
</h:body>
</html>
Upvotes: 0
Views: 192
Reputation: 5087
You are naming the var="BusinessContact"
but trying to use it as "businessContact"
Change this line
<h:dataTable value="#{addressBook.list}" var="BusinessContact">
to this
<h:dataTable value="#{addressBook.list}" var="businessContact">
Upvotes: 1