我希望获得有关联系人的一些信息并将其显示在表格中。我使用了两个由PersonalContact
和BusinessContact
命名的托管bean和一个由Contact
命名的抽象类,包括firstName
,lastName
,ID
,{{ 1}}和email
派生自的属性。也是一个将bean添加到arraylist的泛型类。我把这里只是mphone
作为bean,业务作为XHTML,BusinessContact
作为表和泛型类tableBusiness
,个人文件与它们并行。
从屏幕获取的输入被分配给bean的属性,但是当bean转到要添加的AddressBook
时,其内容将变为null。这是为什么?这是关于范围吗?
这是我的豆子:
Arraylist
这是我的business.xhtml:
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;
}
}
这是我将bean放入arraylist的通用类:
<?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>
最后,这个用于在表格中显示arraylist的xhmtl:
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() {}
}
答案 0 :(得分:1)
您正在命名var="BusinessContact"
但尝试将其用作"businessContact"
更改此行
<h:dataTable value="#{addressBook.list}" var="BusinessContact">
到这个
<h:dataTable value="#{addressBook.list}" var="businessContact">