0

I am showing data in a datatable in primefaces.I have 5 columns.In last column i am showing Edit & Delete button.On click of edit button i am showing the dialog popup but i am not able to display the data of selected row in popup dialog?

Code of xhtml

  <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
    <title>Sale Item</title>
    <link rel="stylesheet" href="css/style.css" media="screen"
        type="text/css" />

</h:head>
<h:body>

    <h:form id="form1">
        <p:dataTable var="transection" value="#{trans.getTransections()}"
            paginator="true" rows="10"
            paginatorTemplate="{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}"
            paginatorPosition="bottom">
            <p:column headerText="number">
                <h:outputText value="#{transection.number}" />

            </p:column>

            <p:column headerText="Amount" width="200">
                <h:outputText value="#{transection.amount}" />
            </p:column>

            <p:column headerText="Type" width="200">
                <h:outputText value="#{transection.type}" />
            </p:column>

            <p:column headerText="Date/Time" width="200">
                <h:outputText value="#{transection.date}" />
            </p:column>
            <p:column headerText="Action" width="200">
                <h:panelGrid columns="2" border="0">
                    <p:commandButton oncomplete="PF('TransDialog').show()"
                        icon="edit-icon">

                        <f:setPropertyActionListener value="#{transection}"
                            target="#{trans.selectedTrans}" />
                    </p:commandButton>
                    <p:commandLink>
                        <p:graphicImage value="images/delete.png"></p:graphicImage>
                    </p:commandLink>

                </h:panelGrid>

            </p:column>
        </p:dataTable>
        <p:dialog header="Transection Detail" widgetVar="TransDialog"
            resizable="false" width="400" showEffect="explode"
            hideEffect="explode">

            <p:panelGrid id="display" columns="2">
                <h:outputText value="Number:" />
                <p:inputText value="#{trans.selectedTrans.number}"/> 

                <h:outputText value="Amount:" />
                <p:inputText value="#{trans.selectedTrans.amount}"/> 

                <h:outputText value="Type:" />
                <p:inputText value="#{trans.selectedTrans.type}"/> 
            </p:panelGrid>
            <h:panelGrid style="margin:0 auto" columns="1">
            <p:commandButton value="Update" actionListener="#{trans.printcal()}"></p:commandButton>

            </h:panelGrid>
        </p:dialog>

    </h:form>
</h:body>
</html>

Code of ManagedBean

   package com.loteria.controller;

import java.util.ArrayList;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.servlet.http.HttpSession;

import com.loteria.beans.Transection;
import com.loteria.model.DBaction;
import com.loteria.util.Util;
@ManagedBean(name="trans")
@SessionScoped
public class TransController {

    ArrayList<Transection> list;
    public ArrayList<Transection> getList() {
        return list;
    }

    public void setList(ArrayList<Transection> list) {
        this.list = list;
    }

    private Transection selectedTrans;

    public Transection getSelectedTrans() {
        return selectedTrans;
    }

    public void setSelectedTrans(Transection selectedTrans) {
        this.selectedTrans = selectedTrans;
    }

    public ArrayList<Transection> getTransections()
    {
    list=new ArrayList<Transection>();
         DBaction db=new DBaction();
         HttpSession session = Util.getSession();
         int uid=Integer.parseInt(session.getAttribute("uid").toString());
         list=db.getAllTransections(uid);
         return list;

    }
    public void printcal()
    {
        System.out.print("princal");
    System.out.print("num is:"+selectedTrans.number);
        System.out.print("amount is"+selectedTrans.amount);
        System.out.print("type is"+selectedTrans.type);

    }
}

Codce of bean calss

   package com.loteria.beans;

public class Transection
{

    public String amount;
    public String number;
    public String date;
    public String type;
    public String status;
    public String tran_num;

    public String getAmount() {
        return amount;
    }
    public void setAmount(String amount) {
        this.amount = amount;
    }
    public String getNumber() {
        return number;
    }
    public void setNumber(String number) {
        this.number = number;
    }
    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
    public String getTran_num() {
        return tran_num;
    }
    public void setTran_num(String tran_num) {
        this.tran_num = tran_num;
    }



}

I get exception

javax.el.PropertyNotFoundException: /User/Records.xhtml @63,57 value="#{trans.selectedTrans.number}": Target Unreachable, 'selectedTrans' returned null

TechChain
  • 7,104
  • 20
  • 81
  • 193
  • Works great in the Primefaces showcases. Find the differences – Kukeltje Apr 10 '15 at 20:14
  • 1
    Oh and make your getTransactions() lazy… see http://stackoverflow.com/questions/2090033/why-jsf-calls-getters-multiple-times – Kukeltje Apr 10 '15 at 20:17
  • Use debug for sure the `public void setSelectedTrans(Transection selectedTrans)` receive not null value – 0x5a4d Apr 11 '15 at 08:46
  • In addition to the accessor method strategy, 1) I would choose `dynamic="true"` (``). 2) You also need `update=":form1:display"` (the `` showing the ``). 3) Since you need to submit the components held by the mentioned ``, you need to enclose them inside another `` - needless to say, "*without nesting ``*". 4) You apparently do not need a verbose session scoped managed in this case. A view scoped bean is much more sufficient to handle this kind of situations. – Tiny Apr 11 '15 at 09:54

1 Answers1

2

TechGuy, the p:dialog is rendered on page load even if it's not immediately visible to the user (check the HTML source). This means that selectedTrans is null when the page is first rendered, resulting in a PropertyNotFoundException. You should be able to fix this error simply by initializing the field in the backing-bean:

private Transection selectedTrans = new Transection();

This will be overwritten by the setPropertyActionListener when you click the button, which is good.

DavidS
  • 4,384
  • 2
  • 23
  • 51