2013-10-14 80 views
0

我只想在某些條件下打印結果數組。 在我的bean中,我有一個方法dataIsOk(),它檢查條件並使用表單中的數據進行計算。計算費用昂貴(約5秒)。方法dataIsOk()被調用多次,然後總時間很長(大約20秒)。多次調用的方法

我是JSF的新手,我不知道如何縮短總時間(例如dataIsOk()只調用一次)。

我XHTML:

<p:outputPanel rendered="#{pBean.dataIsOk() eq true}"> 
    <ui:include src="result.xhtml"/> 
</p:outputPanel> 

我見過@PostContruct但我有一種感覺,它不會在我的情況好了,因爲我的方法從接口需要的數據(再不能在bean的構建之前執行)。

我的功能:

public boolean dataIsOk() { 
    if (profilIsOk()) { 
     Date dateEffet = rechercheDate(); 
     Parametres param = rechercheParam(); 
     FacesContext fc = FacesContext.getCurrentInstance(); 
     EBean eBean = (EBean) fc.getViewRoot().getViewMap().get("eBean"); 
     if (eBean !=null) { 
      Calcul calcul = new Calcul(); 
      List<Tarif> tarifs = new ArrayList<Tarif>(); 
      tarifs = calcul.calculTarif(dateEffet, param, eBean.getType()); 
      return true; 
     } else return false; 
    } 
+0

什麼是'pBean'的範圍是什麼? –

+0

@miroslav_mijajlovic得分視圖 – Fabaud

+0

提供方法'dataIsOk' –

回答

0

你需要重新設計你的代碼。 在您的託管bean創建一個簡單的

private Boolean renderResult;

與它的getter和setter。使用@PostConstruct來初始化renderResult = false。通過ajax調用一個方法checkData後,它實際上會重新計算用戶輸入並更新renderResult的值。即

<p:selectOneMenu id="citytSelection" 
    value="#{pBean.selectedCity}"> 
    <f:selectItem itemLabel="Select your city..." itemValue="" /> 
    <f:selectItems value="#{pBean.cityList}" /> 
    <p:ajax listener="#{pBean.checkData}" update="yourOutputPanelId" /> 
</p:selectOneMenu> 

您的輸出面板看起來像:

<p:outputPanel rendered="#{pBean.renderResult}" id="yourOutputPanelId"> 
    <ui:include src="result.xhtml"/> 
</p:outputPanel> 

你checkData:

public void checkData() { 
    this.renderResult = false; 
    if (profilIsOk()) { 
    Date dateEffet = rechercheDate(); 
    Parametres param = rechercheParam(); 
    FacesContext fc = FacesContext.getCurrentInstance(); 
    EBean eBean = (EBean) fc.getViewRoot().getViewMap().get("eBean"); 
    if (eBean !=null) { 
     Calcul calcul = new Calcul(); 
     List<Tarif> tarifs = new ArrayList<Tarif>(); 
     tarifs = calcul.calculTarif(dateEffet, param, eBean.getType()); 
     this.renderResult = true; 
    } 
    } 
} 
+0

Thx,它的工作原理! – Fabaud