I have a problem to send an object to my ManagedBean in a specific way.
I have a xhtml that needs to show a lot of informations about their content . Their data, are shown in a DataTable with a RowToggler.
That is the rule: I have a Father bean with a list of Son Bean and this with a list of grandson Bean. My screen shows off the Fathers data and in a table their sons data.
Each son is a row with a RowToggler. When you open the expansion, another table will display the grandson (son of Son Bean). In that table on the footer, I Have a <p.commandLink> that will insert a new grandson on my Bean.
So, when I try to create a new grandson, I should pass Who is your SON to the ManagedBean. Thats the problem. I know that is little dificult to understand, but I Will post a functional code that shows the problem.
Im using JSF 2.0 with Primefaces 2.2RC2.
My Beans:
Code: Select all
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class FatherBean implements Serializable {
private String name;
private String lastName;
private List<SonBean> sonsBean;
public FatherBean(String name, String lastName) {
this.name = name;
this.lastName = lastName;
sonsBean = new ArrayList<SonBean>();
}
public FatherBean() {
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<SonBean> getSonsBean() {
return sonsBean;
}
public void setSonsBean(List<SonBean> sonsBean) {
this.sonsBean = sonsBean;
}
}
Code: Select all
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class SonBean implements Serializable {
private String name;
private String lastName;
private List<GrandsonBean> grandsonsBean;
public SonBean(String name, String lastName) {
this.name = name;
this.lastName = lastName;
grandsonsBean = new ArrayList<GrandsonBean>();
}
public SonBean() {
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<GrandsonBean> getGrandsonsBean() {
return grandsonsBean;
}
public void setGrandsonsBean(List<GrandsonBean> grandsonsBean) {
this.grandsonsBean = grandsonsBean;
}
}
Code: Select all
import java.io.Serializable;
public class GrandsonBean implements Serializable {
private String name;
private String lastName;
public GrandsonBean(String name, String lastName) {
this.name = name;
this.lastName = lastName;
}
public GrandsonBean() {
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Code: Select all
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean(name = "testMB")
@ViewScoped
public class commanLinkTestMB implements Serializable {
private FatherBean father;
private SonBean son = new SonBean();
private GrandsonBean grandson = new GrandsonBean();
public commanLinkTestMB() {
this.father = new FatherBean("John", "Doe");
GrandsonBean grandson1 = new GrandsonBean("aaa", "aaaa");
GrandsonBean grandson2 = new GrandsonBean("abb", "abbb");
GrandsonBean grandson3 = new GrandsonBean("acc", "accc");
GrandsonBean grandson4 = new GrandsonBean("bbb", "bbbb");
GrandsonBean grandson5 = new GrandsonBean("bcc", "bccc");
GrandsonBean grandson6 = new GrandsonBean("bdd", "bddd");
SonBean son1 = new SonBean("Son-1", "111");
SonBean son2 = new SonBean("Son-2", "222");
son1.getGrandsonsBean().add(grandson1);
son1.getGrandsonsBean().add(grandson2);
son1.getGrandsonsBean().add(grandson3);
son2.getGrandsonsBean().add(grandson4);
son2.getGrandsonsBean().add(grandson5);
son2.getGrandsonsBean().add(grandson6);
this.father.getSonsBean().add(son1);
this.father.getSonsBean().add(son2);
}
public FatherBean getFather() {
return father;
}
public void setFather(FatherBean father) {
this.father = father;
}
public SonBean getSon() {
return son;
}
public void setSon(SonBean son) {
this.son = son;
}
public GrandsonBean getGrandson() {
return grandson;
}
public void setGrandson(GrandsonBean grandson) {
this.grandson = grandson;
}
public void addGrandson() {
System.out.println("Log -----name - " + son.getName());
System.out.println("Log -----lastName - " + son.getName());
this.son.getGrandsonsBean().add(grandson);
}
}
Code: Select all
<?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://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.prime.com.tr/ui">
<link type="text/css" rel="stylesheet" href="./CSS/Rich/skin.css"/>
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form id="form1">
<p:panel header="Father">
<!-- Panel 1 ###################################-->
<p:panel header="Fathers Info" toggleable="true">
<h:panelGrid columns="1">
<h:panelGrid columns="5">
<h:outputText value="Name:" />
<h:outputText value="#{testMB.father.name}"/>
<p:divider/>
<h:outputText value="LastName:" />
<h:outputText value="#{testMB.father.lastName}"/>
</h:panelGrid>
</h:panelGrid>
</p:panel>
<p:panel header="Sons" toggleable="true">
<p:dataTable id="table" value="#{testMB.father.sonsBean}" var="sons" rows="10" paginatorPosition="bottom" rendered="true">
<p:column style="width:20px">
<p:rowToggler/>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Name" />
</f:facet>
<h:outputText value="#{sons.name}"/>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Last Name" />
</f:facet>
<h:outputText value="#{sons.lastName}"/>
</p:column>
<!--Init Expansion-->
<f:facet name="expansion">
<p:panel header="Grandsons" toggleable="true">
<p:dataTable id="subTable" value="#{sons.grandsonsBean}" var="grand" >
<p:column>
<f:facet name="header">
<h:outputText value="Grandson Name" />
</f:facet>
<h:outputText value="#{grand.name}"/>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Grandson Last Name" />
</f:facet>
<h:outputText value="#{grand.lastName}"/>
</p:column>
<f:facet name="footer">
<p:column>
<p:commandLink id="addlink" oncomplete="addGrandSon.show()" immediate="true">
<h:graphicImage value="/images/icons/add1.png" style="border:0"/>
<f:setPropertyActionListener value="#{sons}" target="#{testMB.son}"/>
</p:commandLink>
</p:column>
</f:facet>
</p:dataTable>
</p:panel>
</f:facet>
<!--End Expansion-->
</p:dataTable>
</p:panel>
</p:panel>
<!-- ################################### -->
<!-- Edition block -->
<p:dialog widgetVar="addGrandSon" width="450" modal="true" header="Add Grandson" resizable="false">
<h:panelGrid columns="2">
<h:outputText value="Name" />
<h:inputText value="#{testMB.grandson.name}"/>
<h:outputText value="Last Name"/>
<h:inputText value="#{testMB.grandson.lastName}"/>
<p:commandButton value="Add"
action="#{testMB.addGrandson}"
update="form1:Grandsons"
oncomplete="addGrandSon.hide();"/>
<p:commandButton value="Cancel"
onclick="addGrandSon.hide();return false;" />
</h:panelGrid>
</p:dialog>
</h:form>
</h:body>
</html>
When the method of my ManagedBean is called, he give a nullPointer exception because my parameter cannot be received from the xhtml.
Seems that this row does not work:
Code: Select all
<f:setPropertyActionListener value="#{sons}" target="#{testMB.son}"/>
I have found a thread with a problem alike but without answers.
http://primefaces.prime.com.tr/forum/vi ... f=3&t=2232
So, If there is a way to solve that problem, please answer me.