p:commandLink doesn't work in nested DataTables

UI Components for JSF
Post Reply
User avatar
Hericksnake
Posts: 47
Joined: 21 Jan 2011, 22:45
Location: Brasil

28 Jan 2011, 12:49

Hello guys,
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;  
    }  
}  
My ManagedBean:

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);  
    }  
}  
The XHTML:

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>  
I tried a lot of ways send the information, like to use the action of the <p:commandLink> or pass the rowIndexVar from the Sons table but the problem persists.
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.
Primefaces 3.2
JSF 2.0
Glassfish Server 3.1.2
Netbeans7.1.1

User avatar
Hericksnake
Posts: 47
Joined: 21 Jan 2011, 22:45
Location: Brasil

31 Jan 2011, 13:41

So, I read another thread and saw a problem like mine.
I'm waiting for the new release of Primefaces.
Seems that if I change the <f:facet expansion> to <p:rowExpansion> (new component) the problem will be solved.
Primefaces 3.2
JSF 2.0
Glassfish Server 3.1.2
Netbeans7.1.1

User avatar
Hericksnake
Posts: 47
Joined: 21 Jan 2011, 22:45
Location: Brasil

01 Feb 2011, 18:01

I confirmed that the problem is solved by replacing the component <f:facet> by the <rowExpansion>.
So, in the next version this will not be a problem anymore.

PS: I did the test in the last nightly build. ;)

Edit: Just to inform, after this the <f:facet expansion> doesn't work anymore.
Primefaces 3.2
JSF 2.0
Glassfish Server 3.1.2
Netbeans7.1.1

smithh032772
Posts: 6144
Joined: 10 Sep 2011, 21:10

16 Dec 2011, 20:01

Click URL below for a possible solution, since p:subTable is similar to, or basically the same thing as, nested DataTables.

Re: [SOLVED] Unable to fire an action from subtable column
Howard

PrimeFaces 6.0, Extensions 6.0.0, Push (Atmosphere 2.4.0)
TomEE+ 1.7.4 (Tomcat 7.0.68), MyFaces Core 2.2.9, JDK8
JUEL 2.2.7 | OmniFaces | EclipseLink-JPA/Derby | Chrome

Java EE 6 Tutorial|NetBeans|Google|Stackoverflow|PrimeFaces|Apache

cagatay.civici
Prime
Posts: 18616
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

16 Dec 2011, 22:30

Try with MyFaces instead of Mojarra.

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 12 guests