打开新页面中数据表中单行的详细信息

时间:2017-07-13 00:57:26

标签: primefaces datatable

我有一个来自数据库的数据表,当我点击“OPEN”按钮时,我希望其中一行的详细信息出现在新表中。{{3} }

当我点击OPEN按钮时,我会收到“未找到记录”,如下面的屏幕截图enter image description here 以下是 index.xhtml

的一部分

//


                                                                  APP WEB

                    <p:ajax event="rowEdit" listener="#{transaction.onRowEdit}" update=":form1:messages" />  
                    <p:ajax event="rowEditCancel" listener="#{transaction.onRowCancel}" update=":form1:messages" /> 

                    <p:column headerText="Reference">
                        <p:commandLink value="#{c.reference}" action="/faces/global/SingleTx.xhtml" target="_blank" >
                            <f:setPropertyActionListener target="#{transaction.showSelectedTx(reference)}" value="#{c}" />
                        </p:commandLink>
                    </p:column>

                    <p:column headerText="Rartner">                               
                             #{c.pname}                                                                              
                    </p:column>
                    <p:column headerText="Status">                                
                            #{c.fk_status}                                                   
                    </p:column>
                    <p:column headerText="Sender">                                                 
                            #{c.sendername}                                 
                    </p:column>

                    <p:column headerText="Beneficiary">                                   
                            #{c.beneficiaryname}                               
                    </p:column>

                    <p:column headerText="Amounts">                            
                             #{c.beneficiary_currency} #{c.beneficiary_amount}                               
                    </p:column>

                    <p:column headerText="Action">
                        <p:commandButton style="float:right" value="Print" >

                            <p:confirm header="#{cd.reference}" message="Are you sure?" icon="ui-icon-alert" /> 
                            <p:button  value="Open" href="/faces/global/SingleTx.xhtml" target="_self" />

                        </p:commandButton>

                    </p:column>
                    <p:column style="width:32px">
                        <p:rowEditor />
                    </p:column>
                </p:dataTable>
                <p:dialog modal="true" width="800px" height="400px" widgetVar="singletx"
                          id="dialog">
                    Name :#{transaction.showSelectedTx(reference)}
                </p:dialog>

            </h:form>

这是支持bean:

package com.mycompany.data;

/**
 *
 * @author bryan
 */
import conn.DBConnector;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "transaction")
@SessionScoped
public class TransactionBean implements Serializable {

    //added
    private static final long serialVersionUID = 1L;
    public String reference;
    public String fk_status;
    public String sendername;
    public String beneficiaryname;
    public String beneficiary_currency;
    public double beneficiary_amount;
    public String pname;
    Transaction user;

    public String getReference() {
        return reference;
    }

    public void setReference(String reference) {
        this.reference = reference;
    }

    public String getFk_status() {
        return fk_status;
    }

    public void setFk_status(String fk_status) {
        this.fk_status = fk_status;
    }

    public String getSendername() {
        return sendername;
    }

    public void setSendername(String sendername) {
        this.sendername = sendername;
    }

    public String getBeneficiaryname() {
        return beneficiaryname;
    }

    public void setBeneficiaryname(String beneficiaryname) {
        this.beneficiaryname = beneficiaryname;
    }

    public String getBeneficiary_currency() {
        return beneficiary_currency;
    }

    public void setBeneficiary_currency(String beneficiary_currency) {
        this.beneficiary_currency = beneficiary_currency;
    }

    public double getBeneficiary_amount() {
        return beneficiary_amount;
    }

    public void setBeneficiary_amount(double beneficiary_amount) {
        this.beneficiary_amount = beneficiary_amount;
    }

    public String getPname() {
        return pname;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }

    //resource injection
    @Resource(name = "jdbc/primefaces")

    //connect to DB and get customer list
    public List<Transaction> getTransactionList() throws SQLException, ClassNotFoundException {

        Connection con = null;

        con = DBConnector.getDBConnection();

        if (con == null) {
            throw new SQLException("Can't get database connection");
        }

        PreparedStatement ps
                = con.prepareStatement(
                        "select tx.reference,p.pname,ts.name, tx.sendername, tx.beneficiaryname, tx.beneficiary_currency,tx.beneficiary_amount from transaction_stage tx join transaction_status ts on tx.fk_status=ts.id join partner p on tx.fk_partner=p.id");

        //get customer data from database
        ResultSet result = ps.executeQuery();

        List<Transaction> list = new ArrayList<Transaction>();

        while (result.next()) {
            Transaction cust = new Transaction();
            cust.setReference(result.getString("reference"));
            cust.setFk_status(result.getString("name"));
            cust.setSendername(result.getString("sendername"));
            cust.setBeneficiaryname(result.getString("beneficiaryname"));
            cust.setBeneficiary_currency(result.getString("beneficiary_currency"));
            cust.setBeneficiary_amount(result.getDouble("beneficiary_amount"));
            cust.setPname(result.getString("pname"));

            //store all data into a List
            list.add(cust);
        }

        return list;
        //added
    }

    //view single transaction
    private List<Transaction> selectSingleTx;

    public Transaction getUser() {
        return user;
    }

    public void setUser(Transaction user) {
        this.user = user;
    }

    public List<Transaction> getSelectSingleTx() {
        return selectSingleTx;
    }

    public void setSelectSingleTx(List<Transaction> selectSingleTx) {
        this.selectSingleTx = selectSingleTx;
    }


    public  void showSelectedTx(String reference){
        Connection con=DBConnector.getDBConnection();
        if(con==null){
            System.out.println("No db connection");
        }
           String ref=getReference();
        try {
            String q="select tx.reference,p.pname,ts.name, tx.sendername, tx.beneficiaryname, tx.beneficiary_currency,tx.beneficiary_amount from transaction_stage tx join transaction_status ts on tx.fk_status=ts.id join partner p on tx.fk_partner=p.id where tx.reference="+ref+"";
            PreparedStatement ps=null;
            ResultSet rs=null;
            ps.setString(1, reference);
            rs=ps.executeQuery();

            display(rs);

        } catch (Exception e) {
        }
    }
    public void selectTx(String reference){

        showSelectedTx(reference);
    }

    // to be implemented later
    private static void display(ResultSet myRs) throws SQLException {
        while (myRs.next()) {
            String sendername = myRs.getString("sendername");
            String beneficiaryname = myRs.getString("beneficiaryname");
            //double salary = myRs.getDouble("salary");
            String department = myRs.getString("department");

            System.out.printf("%s, %s \n", sendername, beneficiaryname);
        }
    }

}

//

1 个答案:

答案 0 :(得分:1)

根据Reference链接,您需要在链接中包含c.reference的值

 value="#{c.reference}"

请参阅JSF ViewParam from CommandButton

How to pass a parameter along with h:commandButton