Jsf 为什么托管Bean列表在sessionscoped中包含空元素?
我想获得一些关于联系人的信息并将其显示在表中。我使用了两个名为Jsf 为什么托管Bean列表在sessionscoped中包含空元素?,jsf,primefaces,Jsf,Primefaces,我想获得一些关于联系人的信息并将其显示在表中。我使用了两个名为PersonalContact和BusinessContact的托管bean,以及一个名为Contact的抽象类,其中包括firstName、lastName、ID、email和mphone属性,这些属性派生自,一个将bean添加到arraylist的泛型类。我在这里把BusinessContact作为一个bean,把业务作为XHTML,把tableBusiness作为一个表和一个通用类AddressBook,personal文件与之
PersonalContact
和BusinessContact
的托管bean,以及一个名为Contact
的抽象类,其中包括firstName
、lastName
、ID
、email
和mphone
属性,这些属性派生自,一个将bean添加到arraylist的泛型类。我在这里把BusinessContact
作为一个bean,把业务作为XHTML,把tableBusiness
作为一个表和一个通用类AddressBook
,personal文件与之平行
从屏幕获取的输入被分配给bean的属性,但当bean转到要添加的Arraylist
时,其内容变为null。为什么呢?是关于示波器的吗
这是我的豆子:
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;
}
}
下面是我的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>
接触
名字:
身份证件:
姓氏:
电邮:
流动电话:
公司名称:
工作电话:
点击查看表格
这是我将bean放入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() {}
}
import java.util.ArrayList;
导入javax.faces.bean.ManagedBean;
导入javax.faces.bean.SessionScoped;
@ManagedBean
@会议范围
公共类通讯簿{
private ArrayList list=new ArrayList();
T-clas;
公共ArrayList getList(){
退货清单;
}
公共无效集合列表(ArrayList列表){
this.list=列表;
}
公共无效添加(T obj){
这个.clas=obj;
//System.out.println(obj.getFirstName()+);
列表。添加(obj);
}
公共无效删除(T obj)
{
列表。删除(obj);
}
/**
*创建AddressBook的新实例
*/
公共通讯簿(){}
}
最后,这个XHTML用于在表中显示arraylist:
<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>
“#{businessContact.firstName}”
名字
姓
#{businessContact.lastName}
手机
#{businessContact.mphone}
电子邮件
#{businessContact.email}
公司名称
#{businessContact.companyName}
工作电话
#{businessContact.workPhone}
您正在命名var=“BusinessContact”
,但试图将其用作“BusinessContact”
换行
<h:dataTable value="#{addressBook.list}" var="BusinessContact">
对此
<h:dataTable value="#{addressBook.list}" var="businessContact">
对不起,它不起作用。我修正了一些东西,但结果是arraylist的整个对象变成了最后添加的对象。