jsf - Why Managed Bean List becomes including null elements in sessionscoped? -
i want information contacts , display them in table. used 2 managed beans named personalcontact
, businesscontact
, 1 abstract class named contact
including firstname
, lastname
, id
, email
, mphone
attributes derives from.also, 1 generic class add beans arraylist. put here businesscontact
bean, business xhtml , tablebusiness
table , generic class addressbook
, files of personal parallel of them.
inputs getting screen assigned attributes of beans when beans go arraylist
added, content becomes null. why that? scopes?
here 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 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 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 blank" value="#{businesscontact.firstname}"> </h:inputtext> id: <h:inputtext id="id" required="true" requiredmessage="can not blank" value="#{businesscontact.id}"> </h:inputtext> last name: <h:inputtext id="lastname" required="true" requiredmessage="can not blank" value="#{businesscontact.lastname}"> </h:inputtext> e-mail: <h:inputtext id="email" required="true" requiredmessage="can not blank" value="#{businesscontact.email}"> </h:inputtext> mobile phone: <h:inputtext id="mphone" required="true" requiredmessage="can not blank" value="#{businesscontact.mphone}"> </h:inputtext> company name: <h:inputtext id="companyname" required="true" requiredmessage="can not blank" value="#{businesscontact.companyname}"> </h:inputtext> work phone: <h:inputtext id="workphone" required="true" requiredmessage="can not 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 table</h:outputlink> <ui:debug hotkey="k" rendered="true"/> </h:form> </h:body>
this generic class putting beans 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 new instance of addressbook */ public addressbook() {} }
and lastly, xhmtl 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>
you naming var="businesscontact"
trying use "businesscontact"
change line
<h:datatable value="#{addressbook.list}" var="businesscontact">
to
<h:datatable value="#{addressbook.list}" var="businesscontact">
Comments
Post a Comment