2012-09-22 19 views
1

如何將對象傳遞給commandButton的動作方法?我使用一個作爲複合組件基礎的數據表。複合組件應該提供交換添加到數據錶行的按鈕的可能性。我認爲我可以通過構面來實現這一點,但是我無法直接通過EL或通過屬性操作偵聽器將對象從數據表列表傳遞到操作方法。JSF2:將列表(dataGrid)中的對象傳遞給commandButton的動作方法

查看:

<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:p="http://primefaces.org/ui" 
    xmlns:customer="http://java.sun.com/jsf/composite/components/customer"> 

<ui:composition template="/WEB-INF/templates/template.xhtml"> 

    <ui:define name="content"> 

    <h:form id="customerList"> 

     <customer:list list="#{customerControllerBean.list}"> 
     <f:facet name="rowButton"> 
      <h:commandButton value="#{msg.deleteButtonLabel}" 
     action="#{customerControllerBean.delete(customer)}" /> 

      <h:commandButton value="#{msg.deleteButtonLabel}" action="#{customerControllerBean.deleteCustomer}"> 
     <f:setPropertyActionListener target="#{customerControllerBean.customer}" value="#{customer}"/> 
      </h:commandButton> 

      <h:button outcome="customerdetail.jsf?id=#{customer.id}" 
     value="#{msg.editButtonLabel}" /> 
     </f:facet> 
     </customer:list> 

    </h:form> 

    </ui:define> 

</ui:composition> 

</html> 

使用下面的複合材料構件customer:list

<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:p="http://primefaces.org/ui" 
    xmlns:composite="http://java.sun.com/jsf/composite"> 

<composite:interface> 
    <composite:attribute name="list" /> 
    <composite:facet name="rowButton" /> 
</composite:interface> 

<composite:implementation> 

    <p:dataTable id="customer" var="customer" value="#{cc.attrs.list}"> 
    ... 
    <p:column> 
     <composite:renderFacet name="rowButton" /> 
    </p:column> 
    </p:dataTable> 

</composite:implementation> 
</html> 

的支持bean:

@Named 
@ConversationScoped 
public class CustomerControllerBean implements Serializable { 

    private static final long serialVersionUID = 6168621124401208753L; 

    List<Customer> allCustomers = null; 
    private Customer customer; 

    // setters and getters ... 

@PostConstruct 
public void loadAllCustomers() { 
    // load customers 
} 

public List<Customer> getList() { 
    return allCustomers; 
} 

    public String delete(Customer customer) { 
     // delete customer... 
     return "deleted"; 
    } 

    public String deleteCustomer() { 
     // delete customer... 
     return "deleted"; 
    } 

是沒可能通過在這種情況下對象的方法?

回答

1

由於您使用的是Primefaces,您可以利用其數據表特性(如屬性和事件)來設置模型。

使用Primefaces'數據表功能

在你的控制器,你可以指定模型和操作要對其執行的,在這種情況下,我用了一個EJB作爲例子。

@ManagedBean 
@ViewScoped 
public class CustomerBean 
{ 
    private Customer model; 

    @EJB 
    private CustomerService cs; 

    public void rowSelected() 
    { 
     // log or do stuff 
    } 

    public void delete() 
    { 
     cs.delete(model); 
    } 

    // getters & setters 
} 

然後你在數據表中指定selectionModeselection性質,與selection映射到你的模型。

<p:dataTable id="dtModel" var="row" value="#{bean.list}" selectionMode="single" selection="#{bean.model}"> 
    <p:ajax event="rowSelect" process="@this" listener="#{bean.rowSelected}" update=":content" /> 
</p:datatable> 

在這種情況下,我使用了聽衆的p:ajax標籤,但它只是如果你想一旦選擇做一些與你的模型可能有用的例子,但它並不需要設置你的模型。您的模型將使用bean的setter方法進行設置。

傳遞行作爲參數

在你的控制器指定接收模型參數的方法。

@ManagedBean 
@ViewScoped 
public class CustomerBean 
{ 
    @EJB 
    private CustomerService cs; 

    public void delete(Customer candidate) 
    { 
     cs.delete(candidate); 
    } 

    // getters & setters 
} 

而在你的數據表使用row對象作爲var屬性指定。

<p:dataTable id="dtModel" var="row" value="#{bean.list}"> 
    <p:column headerText="name"> 
     <h:outputText value="#{row.name}" /> 
    </p:column> 
    <p:column headerText="delete"> 
     <p:commandButton value="delete" actionListener="#{bean.delete(row)}" /> 
    </p:column> 
</p:datatable> 

我更喜歡使用的第一個例子,因爲它允許用戶通過選擇一個行,然後就可以執行各種操作,如刪除,編輯設置模型或查看其詳細信息。你可以啓用或如果選擇與否的模型,通過檢查禁用您的UI按鈕等...

提示

而是複合組件,你可以使用簡單的Facelets模板來創建一個簡單的標籤庫。它們通常工作正常,唯一的缺點是無法強制使用這些組件的接口。

你也可以檢查PrimeFaces showcase,它有很多有用的例子。

我希望它有幫助。

+0

感謝您的提示。基本上我可以將對象傳遞給動作方法。當我添加一個攜帶數據表的複合組件時,以及當我使用facet添加按鈕時,我遇到了問題。無論如何,我嘗試了兩個例子。我還添加了rowKey,以便編譯示例。第一個示例不起作用,當調用模型的setter時,setter的參數爲null。第二個例子工作,但只有當我將按鈕直接添加到數據表。當我通過facet添加它時,它不起作用,bean.delete(row)方法的參數行也是null。 – Joysn

+1

我明白了,你真的需要使用複合組件嗎?我的意思是,前一段時間我試圖使用它們,花費了太多時間,並且發現最簡單的場景不會工作,因爲spec和impl中有很多錯誤。我最終使用了作爲組件工作的簡單模板,創建了一個簡單的taglib。效果很好,但缺點是您無法強制組件的接口。問候! – rbento

+0

好..我正在尋找一個優雅的解決方案,以某種方式'配置'按鈕。數據表中的一行。一種情況是有一個刪除和一個編輯按鈕,一次是有一個視圖和選擇按鈕,所以我可以選擇一個實體在其他實體中設置...並且這些按鈕在不同的bean上調用不同的方法。我認爲方面將是一個優雅的方式來實現這一目標。 – Joysn

相關問題