所以为了让它相对简单:我有一些 Primefaces-Page,它应该以表结构表示数据库记录。
我将这些记录保存List<Customer>
在一个@ConversationScoped
支持 bean 中。我已通过调试验证,列表已正确填充数据库中的记录(hibernate db FWIW)。这是在“businessInterface”Distributor 类的帮助下完成的,它实际上只是(数据库)服务类的解耦掩码。
如前所述,我已经验证了数据库以及分发器正确地返回了预期值。不幸的是,在视图中没有记录,而是emptyMessage
显示。
该应用程序在 jBoss 7.1.1-Final 应用服务器上运行。
为了更好的可读性,我排除了提供的代码周围的h:head
, h:body
, ui:composition
,ui:define
和h:form
,并将列缩短为两种不同的用法(属性显示和动作说明)
视图(缩短和截断):
<ui:define name="inhalt">
<p:growl id="msgGrowl" autoUpdate="true" showDetail="true" />
<h:form onkeypress="if (event.keyCode == 13) {return false; }">
<p:dataTable var="customeritem" id="customerTable"
rowkey="#{customeritem.id}" value="#{customerListController.customerList}"
paginator="true" rows="13" autoUpdate="true"
filteredValue="#{customerListController.filteredCustomers}"
emptyMessage="no customers found!"
sortFunction="#{customerListController.filteredCustomers}">
<p:column sortBy="name" filterBy="name" headerText="Kunde"
filterMatchMode="contains">
<h:outputText value="#{customeritem.name}" />
</p:column>
<p:column>
<f:facet name="header">
<p:commandButton value="Neuer Kunde"
action="${customerListController.addCustomer()}"
icon="ui-icon-plus" />
</f:facet>
<p:commandButton id="doViewDetailsButton" icon="ui-icon-clipboard"
action="${customerListController.viewDetails(customeritem.getId())}" />
<p:tooltip for="doViewDetailsButton" value="Details ansehen" />
</p:column>
</p:dataTable>
</h:form>
</ui:define>
支持豆:
@Named
@ConversationScoped
public class CustomerListController implements Serializable {
private static final long serialVersionUID = -5961625401284927892L;
private List<Customer> customerList = new ArrayList<Customer>();
private List<Customer> filteredCustomers = new ArrayList<Customer>();
@Inject
CustomerEditController customerEditController;
@Inject
CustomerDetailsController customerDetailsController;
@Inject
CustomerDistributor businessInterface;
public String addCustomer() {
return editCustomer(0l);
}
public String editCustomer(long customerId) {
setFilteredCustomers(null);
customerEditController.recieveCustomerById(customerId);
return Pages.CUSTOMER_EDIT;
}
public String viewDetails(long customerId) {
setFilteredCustomers(null);
customerDetailsController.recieveCustomerById(customerId);
return Pages.CUSTOMER_DETAILS;
}
public String deleteCustomer(long customerIdToDelete) {
businessInterface.delete(customerIdToDelete);
setFilteredCustomers(null);
fillCustomerList();
return Pages.CUSTOMER_LIST;
}
@PostConstruct
public void fillCustomerList() {
customerList.clear();
customerList.addAll(businessInterface.loadAll());
}
public List<Customer> getCustomerList() {
return customerList;
}
public List<Customer> getFilteredCustomers() {
return filteredCustomers;
}
public void setFilteredCustomers(List<Customer> filteredCustomers) {
this.filteredCustomers = filteredCustomers;
}
}
这曾经可以工作,当我有 Backing Bean 时@SessionScoped
,但由于这需要 hackish 变通方法来产生直观(和预期)的行为,我决定将 Backing Bean 移动到更小的范围。因此,我选择了@ConversationScoped
,因为 BackingBean 需要比请求生命周期保持更长的时间......(此外,针对每个请求对数据库运行查询非常昂贵......)
关于CustomerEditController
and的简短说明CustomerDetailsController
。如果通过单击其中一个按钮提出请求,他们负责编辑和显示有关单个记录的更多信息。
非工作的东西是@PostConstruct public void fillCustomerList()
. 其他一切都按预期工作......
如果您需要任何进一步的信息,请询问,我会根据需要提供上下文;)