2014-12-06 89 views
0

我想获得一些关于联系人的信息并将它们显示在表格中。我使用PersonalContactBusinessContact和一个抽象类名为命名为Contact两个托管豆类包括其派生from.Also,一个泛型类豆子添加到一个ArrayList firstNamelastNameIDemailmphone属性。我把这里只是BusinessContact作为一个bean,作为XHTML的业务,tableBusiness作为一个表和泛型类AddressBook,个人的文件是平行的。为什么Managed Bean List在sessionscoped中包含空元素?

从屏幕获取的输入被分配给bean的属性,但是当bean去往要添加的Arraylist时,其内容变为空。这是为什么?这是关于范围?

这里是我的豆:

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> 

这是我把豆子到一个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() {}   
} 

A ND最后,这XHMTL在表显示的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> 

回答

1

您命名var="BusinessContact"但是,试图用它作为"businessContact"

改变这一行

<h:dataTable value="#{addressBook.list}" var="BusinessContact"> 

这个

<h:dataTable value="#{addressBook.list}" var="businessContact"> 
+0

对不起,它不起作用。我固定了一些东西,但结果是arraylist的整个对象成为最后添加的对象。 – 2014-12-08 00:04:06