0

I would like to print an array of results only under some conditions. In my bean, I have a method dataIsOk() which check the conditions and do the computation with data from a form. Computation is costly (around 5 seconds). The method dataIsOk() is called several times and then the total time is large (around 20 seconds).

I am new in JSF and I don't what to do for having a smaller total time (for instance dataIsOk() only called once).

my xhtml:

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

I have seen @PostContruct but I have the feeling that it won't be ok in my case, because my method needs data from the interface (and then can't be executed before the construction of the bean).

My function :

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;
     }
Joffrey Hernandez
  • 1,809
  • 3
  • 21
  • 39
Fabaud
  • 141
  • 2
  • 11
  • What is the scope of `pBean`? – miroslav_mijajlovic Oct 14 '13 at 13:17
  • @miroslav_mijajlovic the score view – Fabaud Oct 14 '13 at 13:27
  • Provide method `dataIsOk` – miroslav_mijajlovic Oct 14 '13 at 13:28
  • 2
    possible duplicate of [Why JSF calls getters multiple times](http://stackoverflow.com/questions/2090033/why-jsf-calls-getters-multiple-times) – sleske Oct 14 '13 at 13:35
  • JSF reserves the right to call getters on backing beans multiple times. See the linked question for details. – sleske Oct 14 '13 at 13:36
  • Another dupe: [Why is the getter called so many times by the rendered attribute?](http://stackoverflow.com/questions/4281261/why-is-the-getter-called-so-many-times-by-the-rendered-attribute) – sleske Oct 14 '13 at 13:37
  • Yes I know it calls it several times. I just want to know if it's possible to avoid that. I don't know how to do that. – Fabaud Oct 14 '13 at 13:57
  • I have edited with the function. – Fabaud Oct 14 '13 at 13:58
  • Did you read either of the links which sleske provided above? It goes into great detail about your exact problem, what can be done, and what cannot be done. – patstuart Oct 14 '13 at 14:02
  • Yes. I will read it again but I haven't understand/seen where it's said what can be done. – Fabaud Oct 14 '13 at 14:15
  • Here you can see the issue https://java.net/jira/browse/JAVASERVERFACES-1253?page=com.atlassian.jira.plugin.system.issuetabpanels%3Achangehistory-tabpanel – miroslav_mijajlovic Oct 14 '13 at 14:24
  • What don't you understand? BalusC explained that what you're doing can't be done with getters, and he gave the alternative for how you should do it instead. – patstuart Oct 14 '13 at 14:26
  • I have had the @PostConstruct specification with my dataIsOk method in it ; but it won't work because this is called during the loading of the webpage, so I haven't the data. In my xhtml page, I have a form with a button, and when I click on the button I have the results on the same page. – Fabaud Oct 14 '13 at 14:37
  • Your best bet then is to use an `actionListener` on the button. Those will only be called one time. – patstuart Oct 14 '13 at 14:46

1 Answers1

0

You need to redesign your code. In your managed bean create a simple

private Boolean renderResult;

with it's getters and setters. Use a @PostConstruct to initialize renderResult = false. After you call through ajax a method checkData that actually will recalculate the user input and update the value of renderResult. i.e.

<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>

Your output panel will look like:

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

Your 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;
    }
  }
}
Yamada
  • 723
  • 6
  • 23