无法在Primefaces的弹出对话框中显示值?

时间:2015-04-10 19:43:40

标签: jsf jsf-2 primefaces datatable

我在primeface中的数据表中显示数据。我有5列。在最后一栏中,我显示了Edit&删除按钮。单击编辑按钮我显示对话框弹出,但我无法在弹出对话框中显示所选行的数据?

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>

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);

    }
}

bean calss的Codce

   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;
    }



}

我得到例外

javax.el.PropertyNotFoundException:/User/Records.xhtml @ 63,57 value =“#{trans.selectedTrans.number}”:目标无法访问,'selectedTrans'返回null

1 个答案:

答案 0 :(得分:2)

TechGuy,p:dialog在页面加载时呈现,即使用户不能立即看到它(检查HTML源代码)。这意味着首次呈现页面时selectedTrans为null,从而导致PropertyNotFoundException。您应该只需通过初始化backing-bean中的字段来修复此错误:

private Transection selectedTrans = new Transection();

单击按钮时,setPropertyActionListener将覆盖此内容,这很好。