我正在做一个例子来从mysql数据库中检索数据列表。这一切都很好。 加载页面时出现问题,并且数据恢复方法在第一次加载页面时运行两次。 当我从dataTable方法中选择一条记录时,从列表中检索数据会被执行多次,具体取决于所选记录。例如,如果我选择4号注册,它将运行4次,直到达到所选索引,以显示该记录的详细信息。 任何想法行为?如何解决方法运行一次? Primefaces 5.0.RC2 + Spring4,JPA,Annotations
package com.maosdel.rgi.web.controllers;
import java.io.Serializable;
import java.util.List;
import javax.faces.bean.SessionScoped;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.maosdel.rgi.dto.OperatorDTO;
import com.maosdel.rgi.model.exception.RgiBusinessException;
import com.maosdel.rgi.model.services.OperatorService;
@SessionScoped
@Component("operatorBean")
public class OperatorBean implements Serializable {
static final Logger logger = LoggerFactory.getLogger(OperatorBean.class);
private static final long serialVersionUID = -491242034992898914L;
@Autowired(required = true)
private OperatorService operatorService;
private OperatorDTO operatorSelected;
private List<OperatorDTO> operatorList;
public OperatorBean() {
logger.info("OperatorBean instantiated...");
}
private List<OperatorDTO> loadAllOperators() {
List<OperatorDTO> result = null;
try {
result = (List<OperatorDTO>) operatorService.getAllOperatorsHQL();
} catch (RgiBusinessException e) {
logger.error("Error recuperando registros... " + e);
}
return result;
}
public List<OperatorDTO> getOperatorList() {
operatorList = loadAllOperators();
return operatorList;
}
public OperatorDTO getOperatorSelected() {
try {
if (null != operatorSelected) {
operatorSelected = operatorService.getOperatorById(operatorSelected.getId());
}
} catch (RgiBusinessException e) {
logger.error("Error recuperando operador con Id: " + 2 + " Error: "
+ e);
}
return operatorSelected;
}
public void setOperatorSelected(OperatorDTO operatorSelected) {
this.operatorSelected = operatorSelected;
}
}
xhtml页面
<ui:composition template="/WEB-INF/templates/defaultTemplate.xhtml">
<ui:define name="content">
<h:form id="operatorFrm">
<p:growl id="msgs" showDetail="true" />
<p:dataTable var="operator" value="#{operatorBean.operatorList}"
paginator="true" rows="10" rowKey="#{operator.id}"
selectionMode="single">
<f:facet name="header">
Operadores
</f:facet>
<p:column headerText="Id">
<h:outputText value="#{operator.id}" />
</p:column>
<p:column headerText="Code">
<h:outputText value="#{operator.code}" />
</p:column>
<p:column headerText="Name">
<h:outputText value="#{operator.name}" />
</p:column>
<p:column headerText="Last name">
<h:outputText value="#{operator.lastName}" />
</p:column>
<p:column headerText="Data contract">
<h:outputText value="#{operator.contractDate}" />
</p:column>
<p:column headerText="State">
<h:outputText value="#{operator.state}" />
</p:column>
<p:column style="width:32px;text-align: center">
<p:commandButton update=":operatorFrm:operatorDetail"
oncomplete="PF('operatorDlg').show()" icon="ui-icon-search"
title="View">
<f:setPropertyActionListener value="#{operator}"
target="#{operatorBean.operatorSelected}" />
</p:commandButton>
</p:column>
</p:dataTable>
<p:dialog header="Operator Info" widgetVar="operatorDlg" modal="true"
showEffect="fade" hideEffect="fade" resizable="false">
<p:outputPanel id="operatorDetail" style="text-align:center;">
<p:panelGrid columns="2"
rendered="#{not empty operatorBean.operatorSelected}"
columnClasses="label,value">
<f:facet name="header">
</f:facet>
<h:outputText value="Id:" />
<h:outputText value="#{operatorBean.operatorSelected.id}" />
<h:outputText value="Code" />
<h:outputText value="#{operatorBean.operatorSelected.code}" />
<h:outputText value="Name" />
<h:outputText value="#{operatorBean.operatorSelected.name}" />
<h:outputText value="Last name" />
<h:outputText value="#{operatorBean.operatorSelected.lastName}" />
<h:outputText value="Data contract" />
<h:outputText value="#{operatorBean.operatorSelected.contractDate}" />
<h:outputText value="State" />
<h:outputText value="#{operatorBean.operatorSelected.state}" />
</p:panelGrid>
</p:outputPanel>
</p:dialog>
</h:form>
</ui:define>
答案 0 :(得分:0)
public List<OperatorDTO> getOperatorList() {
operatorList = loadAllOperators();
return operatorList;
}
这是你的问题。数据表组件正在调用列表的getter,我也假设它正在进行数据库调用。这也被认为是糟糕的设计,因为访问器方法不应该做那样的操作。
因此,您需要做的是初始化列表并在开头而不是在getter中加载数据。
如果您尚未使用@PostConstruct初始化列表,您还可以查看该展示。
http://www.primefaces.org/showcase/ui/data/datatable/selection.xhtml
<强>更新强>
<h:outputText value="Id:" />
<h:outputText value="#{operatorBean.operatorSelected.id}" />
<h:outputText value="#{operatorBean.operatorSelected.code}" />
<h:outputText value="#{operatorBean.operatorSelected.name}" />
<h:outputText value="#{operatorBean.operatorSelected.lastName}" />
<h:outputText value="#{operatorBean.operatorSelected.contractDate}" />
<h:outputText value="#{operatorBean.operatorSelected.state}" />
上面代码的每一行都在你的支持bean中调用getter getOperatorSelected(),这就是为什么方法执行多次,这是正常的行为。但是,您的方法再次执行不必要的操作。访问器方法应该只做一件事,即返回值。
public OperatorDTO getOperatorSelected() {
return operatorSelected;
}
您不必使用服务方法手动执行查找,数据表将为您处理。
答案 1 :(得分:0)
您忘记了选择标记
selection="#{operatorBean.operatorSelected}"
你可以试试下面的代码吗?
<p:dataTable var="operator" value="#{operatorBean.operatorList}"
paginator="true" rows="10" rowKey="#{operator.id}"
selectionMode="single" selection="#{operatorBean.operatorSelected}">