简体   繁体   English

为什么“受管Bean列表”在sessionscoped中变得包含空元素?

[英]Why Managed Bean List becomes including null elements in sessionscoped?

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. 我使用了两个由PersonalContactBusinessContact命名的托管bean和一个由Contact命名的抽象类,其中包括mphone派生的firstNamelastNameIDemailmphone属性。还有一个通用类将bean添加到数组列表中。 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. 我在这里仅将BusinessContact作为一个bean,将Business作为XHTML和tableBusiness作为一个表以及泛型类AddressBook ,personal文件与它们平行。

Inputs getting from screen are assigned to attributes of beans but when the beans go to Arraylist to be added, its content becomes null. 从屏幕获取的输入被分配给bean的属性,但是当bean进入要添加的Arraylist ,其内容将为空。 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: 这是我的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: 这是用于将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() {}        
}

And lastly, this xhmtl for displaying arraylist in table: 最后,此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>

You are naming the var="BusinessContact" but trying to use it as "businessContact" 您正在命名var="BusinessContact"但尝试将其用作"businessContact"

Change this line 更改此行

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

to this 对此

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么可以将@Stateless EJB注入@SessionScoped Managed Bean? - Why is it possible to @Inject a @Stateless EJB into a @SessionScoped Managed Bean? JSF-访问SessionScoped托管bean - JSF - Accessing a SessionScoped managed bean JSF SessionScoped托管bean - 重启服务器后,spring bean的注入为null - JSF SessionScoped managed bean - injection of spring bean is null after restarting server sessionscoped托管bean与有状态ejb - sessionscoped managed bean vs stateful ejb Sessionscoped托管bean不保存jsf变量 - Sessionscoped managed bean not saving variables jsf JSF:会话作用域托管bean的属性变为null - JSF: Attribute of session scoped managed bean becomes null SessionScoped Managed Bean 值未从 Servlet Filter 更改 - SessionScoped Managed Bean value not changed from Servlet Filter 由PrimeExceptionHandler处理异常时,将重新创建@SessionScoped受管Bean - @SessionScoped managed bean recreated when exception is handled by PrimeExceptionHandler 为什么不能在另一个bean的构造函数中获得SessionScoped bean的值? - Why cant I get the value of a SessionScoped bean in the constructor of another bean? 将@SessionScoped CDI bean注入JSF @ManagedBean后,实例为null - Instance null after injecting @SessionScoped CDI bean into a JSF @ManagedBean
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM