有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Primefaces 4.0数据表对话框详细信息不起作用

我使用的是PrimeFaces4.0和Netbeans 6.9.1。我在这里跟随了primefaces演示:

DataTable Single Selection

除按钮视图外,一切正常。这是我的密码:

客户列表。xhtml

<p:growl id="msgs" showDetail="true" />
<h:form id="formTable">
    <p:dataTable styleClass="table" id="customers" var="customer" value="#{customerBean.customer}">

        <p:column>
            <f:facet name="header">First Name</f:facet>
            #{customer.firstName}
        </p:column>

        <p:column>
            <f:facet name="header">Last Name</f:facet>
            #{customer.lastName}
        </p:column>

        <p:column>
            <f:facet name="header">Email</f:facet>
            #{customer.email}
        </p:column>

        <p:column>
            <f:facet name="header">DOB</f:facet>
            #{customer.dob}
        </p:column>
        <p:column style="width:4%">
            <p:commandButton id="selectButton" update=":formCreate" oncomplete="dialogCustomerCreate.show()" icon="ui-icon-search" title="Update">
                <f:setPropertyActionListener value="#{customer}" target="#{customerBean.selectedCustomer}" />
            </p:commandButton>
        </p:column>
    </p:dataTable>
</h:form>
<h:form id="formCreate">
    <p:dialog header="Create New Customer" widgetVar="dialogCustomerCreate" resizable="false" id="dlgCustomerCreate"
              showEffect="fade" hideEffect="explode" modal="true">

        <h:panelGrid id="display" columns="2" cellpadding="4" style="margin:0 auto;">

            <h:outputText value="First Name:" />
            <h:outputText value="#{customerBean.selectedCustomer.firstName}" style="font-weight:bold"/>

            <h:outputText value="Last Name:" />
            <h:outputText value="#{customerBean.selectedCustomer.lastName}" style="font-weight:bold"/>


            <h:outputText value="Email:" />
            <h:outputText value="#{customerBean.selectedCustomer.email}" style="font-weight:bold"/>

            <h:outputText value="DOB:" />
            <h:outputText value="#{customerBean.selectedCustomer.dob}" style="font-weight:bold"/>

        </h:panelGrid>

    </p:dialog>

</h:form>

客户bean。java

public class customerBean {

    private List<Customer> customer;
    private Customer selectedCustomer;

    /** Creates a new instance of customerBean */
    public customerBean() {
        customer = new ArrayList<Customer>();        
    }

    public List<Customer> getCustomer() {
        CustomersDao cust_dao = new CustomersDao();
        customer = cust_dao.findAll();
        return customer;
    }

    public Customer getSelectedCustomer() {
        return selectedCustomer;
    }

    public void setSelectedCustomer(Customer selectedCustomer) {
        this.selectedCustomer = selectedCustomer;
    }
}

客户道。java

public class CustomersDao {
    public List<Customer> findAll(){
        List<Customer> list_cust = null;
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        String sql = "FROM Customer";
        try{
            session.beginTransaction();
            list_cust = session.createQuery(sql).list();
            session.beginTransaction().commit();
        }catch(Exception e){
            session.beginTransaction().rollback();
        }
        return list_cust;
    }
}

希望有人能告诉我我的代码有什么问题。我花了两天的时间来解决它。谢谢你的阅读


共 (3) 个答案

  1. # 1 楼答案

    1)尝试删除<h:form/>,因为您没有在该对话框中提交任何信息

    2)尝试将<p:commandButton update=""/>更改为update=":display"

    3)将process="@form"添加到你的<p:commandButton/>

    4)如果那样行得通,我想你不需要这个

  2. # 2 楼答案

    您的客户类别需要有一个转换器:

    @FacesConverter(forClass = Customer.class)
    public class CustomerConverter implements Converter {
    
        @Override
        public Object getAsObject(FacesContext context, UIComponent component, String value) {
            if (value != null && !value.equals("") && !value.equals("0")) {
                //find and return object from DAO
            } else {
                return null;
            }
        }
    
        @Override
        public String getAsString(FacesContext context, UIComponent component, Object value) {
            //return object id as string
       }
    }
    
  3. # 3 楼答案

    您没有提到在commandButton属性update处显示弹出对话框的正确id

    <p:commandButton id="selectButton" update=":formCreate:display" oncomplete="dialogCustomerCreate.show()" icon="ui-icon-search" title="Update">
         <f:setPropertyActionListener value="#{customer}" target="#{customerBean.selectedCustomer}" />
    </p:commandButton>