【问题标题】:Primefaces 4.0 dataTable dialog details doesn't workingPrimefaces 4.0 dataTable 对话框详细信息不起作用
【发布时间】:2013-12-03 06:00:59
【问题描述】:

我正在使用 PrimeFaces 4.0 和 Netbeans 6.9.1。我在这里关注了 primefaces 演示:

DataTable Single Selection

一切正常,期待按钮视图。这是我的代码:

Customer_list.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>

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

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

希望有人建议我的代码有什么问题。我需要 2 天才能解决它。感谢阅读!

【问题讨论】:

  • 你有客户类的转换器吗?
  • @fareed 我正在使用 Hibernate,所以它生成了实体客户。
  • 尝试为我在答案中提到的客户类添加转换器。

标签: java jsf web-applications primefaces datatable


【解决方案1】:

您需要为您的 Customer 类提供转换器:

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

【讨论】:

  • 我认为不需要创建转换器类。
【解决方案2】:

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

2) 尝试将您的&lt;p:commandButton update=""/&gt; 更改为update=":display"

3) 将process="@form" 添加到您的&lt;p:commandButton/&gt;

4) 如果可行,我认为您不需要 &lt;h:form/&gt;

【讨论】:

    【解决方案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>
    

    【讨论】:

    • 将 p:dialog 组件移到 p:dataTable 组件下,并将属性 id 更新为 ":formTable:display"。您使用两种形式,使用单一形式。
    • 我将多个表单用于多种用途以及更新、删除、创建。
    • @thehung1724 使用单一表格一次,检查问题是否可以解决。
    • @thehung1724 您正在使用哪个服务器来运行此应用程序
    猜你喜欢
    • 2013-07-02
    • 1970-01-01
    • 1970-01-01
    • 2013-09-10
    • 1970-01-01
    • 2013-04-03
    • 1970-01-01
    • 2020-03-29
    • 1970-01-01
    相关资源
    最近更新 更多