我使用primefaces 5.1制作了一个复合组件。我想要一个对话框显示,你可以输入一个名字并搜索数据库。然后,您可以选择一个条目,然后再次关闭对话框。我的问题是dataTable中的选择总是返回null ...
这是复合材料:
<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:rich="http://richfaces.org/rich"
xmlns:a4j="http://richfaces.org/a4j" xmlns:p="http://primefaces.org/ui"
xmlns:composite="http://java.sun.com/jsf/composite">
<composite:interface componentType="searchCustomer">
<composite:attribute name="customerCache" required="true"
type="com.hji.tis.ui.cache.customer.CustomerCache" />
<composite:attribute name="useExtendedCustomersOnly" default="false" />
</composite:interface>
<composite:implementation>
<p:commandLink styleClass="ui-icon ui-icon-search"
id="searchCustomerIcon" onclick="PF('searchCustomerDialog').show();" />
<p:tooltip for="searchCustomerIcon"
value="#{msgs['request.searchByCustomerNumber']}" />
<p:dialog header="#{msgs['general.searchCustomer']}" resizable="false"
draggable="false" width="500" height="450" modal="true"
appendTo="@(body)" widgetVar="searchCustomerDialog">
<p:panel id="searchCustomerDialogContent" style="width:100%">
<p:panelGrid style="width:480px">
<p:row>
<p:column style="width:60px">
<p:graphicImage value="/resources/images/userSearch.png"
width="50" height="50" />
</p:column>
<p:column style="width:100px">
<p:outputLabel value="#{msgs['general.customerName']}" />
</p:column>
<p:column style="width:160px">
<p:inputText value="#{cc.custSearch_name}"
id="input_searchCustomerName" />
</p:column>
<p:column>
<p:commandLink styleClass="ui-icon ui-icon-search"
action="#{cc.findCustomersByName}"
update="searchCustomerDialogContent"
process="@this, input_searchCustomerName" partialSubmit="true" />
</p:column>
</p:row>
<p:row>
<p:column colspan="4">
<p:dataTable value="#{cc.filteredCustomers}" var="customer"
scrollable="true" scrollHeight="300"
emptyMessage="#{msgs['general.noSelection']}"
selection="#{cc.selectedCustomer}" rowKey="#{customer.id}"
selectionMode="single">
<p:ajax event="rowSelect" process="@this" />
<p:column headerText="#{msgs['general.customerName']}"
styleClass="unmarkable">
<h:outputText value="#{customer.fullName}"
rendered="#{!customer.extendedCustomer}" />
<h:outputText value="#{customer.name}"
rendered="#{customer.extendedCustomer}" />
</p:column>
<p:column headerText="#{msgs['general.address']}"
styleClass="unmarkable">
<h:outputText
value="#{customer.homeAddress.address.country}, #{customer.homeAddress.address.zipCode} #{customer.homeAddress.address.city}, #{customer.homeAddress.address.street} #{customer.homeAddress.address.houseNr}" />
</p:column>
</p:dataTable>
</p:column>
</p:row>
<p:row>
<p:column colspan="4">
<p:commandButton icon="ui-icon-check" style="float:right"
action="#{cc.test}" process="@this, searchCustomerDialogContent"
partialSubmit="true" />
</p:column>
</p:row>
</p:panelGrid>
</p:panel>
</p:dialog>
</composite:implementation>
</html>
这是Faces组件:
package com.hji.tis.ui.util.customComponents;
import java.io.IOException;
import java.util.List;
import javax.faces.component.FacesComponent;
import javax.faces.component.UINamingContainer;
import javax.faces.context.FacesContext;
import lombok.Getter;
import lombok.Setter;
import org.primefaces.event.SelectEvent;
import com.hji.tis.domain.model.customer.Customer;
import com.hji.tis.ui.cache.customer.CustomerCache;
/**
* NamingContainer for the custom element searchCustomer.
*
* @author mayra
*
*/
@FacesComponent("searchCustomer")
public class SearchCustomer extends UINamingContainer {
private final String CUSTOMER_CACHE = "customerCache";
private final String USER_EXTENDED_CUSTOMERS_ONLY = "useExtendedCustomersOnly";
private final String FILTERED_CUSTOMERS = "filteredCustomers";
private final String SELECTED_CUSTOMER = "selectedCustomer";
@Getter
@Setter
private String custSearch_name;
@Getter
@Setter
private Customer custSearch_selectedCustomer;
@Override
public void encodeBegin(FacesContext facesContext) throws IOException {
initCustomerCache();
initUseExtendedCustomersOnly();
super.encodeBegin(facesContext);
}
public void onRowSelect(SelectEvent event) {
System.out.println(((Customer) (event.getObject())).getFullName());
}
/**
* finds the customer by the name set to the _Name field. Decides wether
* only private Customers should be returned or extended.
*/
public void findCustomersByName() {
this.custSearch_selectedCustomer = null;
if (useExtendedCustomersOnly()) {
setFilteredCustomers(getCustomerCache().findExtendedCustomerEntriesByName(custSearch_name));
} else {
setFilteredCustomers(getCustomerCache().findAllCustomerEntriesByName(custSearch_name));
}
}
private boolean useExtendedCustomersOnly() {
return getStateHelperValue(USER_EXTENDED_CUSTOMERS_ONLY);
}
private CustomerCache getCustomerCache() {
return getStateHelperValue(CUSTOMER_CACHE);
}
public Customer getSelectedCustomer() {
return (Customer) this.getStateHelper().get(SELECTED_CUSTOMER);
}
public void setSelectedCustomer(Customer customer) {
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!HERE THE CUSTOMER IS ALWAYS NULL
this.getStateHelper().put(SELECTED_CUSTOMER, customer);
}
public List<Customer> getFilteredCustomers() {
return (List<Customer>) this.getStateHelper().get(FILTERED_CUSTOMERS);
}
private void setFilteredCustomers(List<Customer> customers) {
this.getStateHelper().put(FILTERED_CUSTOMERS, customers);
}
/**
* stores the customerCache in the state helper.
*/
private void initCustomerCache() {
CustomerCache customerCache = getAttributeValue(CUSTOMER_CACHE);
this.getStateHelper().put(CUSTOMER_CACHE, customerCache);
}
private void initUseExtendedCustomersOnly() {
String value = getAttributeValue(USER_EXTENDED_CUSTOMERS_ONLY);
boolean boolValue = false;
if (value.equalsIgnoreCase("TRUE")) {
boolValue = true;
} else {
boolValue = false;
}
this.getStateHelper().put(USER_EXTENDED_CUSTOMERS_ONLY, boolValue);
}
/**
* Return specified attribute value or otherwise the specified default if
* it's null.
*/
@SuppressWarnings("unchecked")
private <T> T getAttributeValue(String key) {
return (T) this.getAttributes().get(key);
}
@SuppressWarnings("unchecked")
private <T> T getStateHelperValue(String key) {
return (T) this.getStateHelper().get(key);
}
}
也许有人知道原因?
谢谢!