0

In the view main_app.xhtml there are 5 different fields, each of them contains a button, when clicking in this button it has to open the view chart_xxx.xhtml, and it will show a chart with the different data related to the field i.e.:

General Rating will show some data in the Chart Clean Rating will show some different data in the Chart etc

When I click the button the buildGraph() method is called several times, usually 2 or 3, and it some of those requests the parameter sent in the request is shown and in other is not, I don't know why this is happening, therefore I decided to store that parameter sent by the request in the session but... I don't know why, if I go back to main_app.xhtml, click in a different button, the view chart_xxx.xhtml will show the data from the previous request sent, but if I go back again to main_app.xhtml and click again in the same button, the correct data is shown. For example:

  1. I click on General Rating button, its data is shown on the chart.
  2. Go back to main_app.xhtml
  3. Click on Clean rating
  4. Data from General Rating is shown.
  5. Go back to main_app.xhtml
  6. Click on Clean rating again
  7. Data from Clean rating is shown correctly

I have read this topic Why JSF calls getters multiple times several times but I couldn't find a way to adapt it to my feature. Furthermore, if I use @RequestScoped, the chart is shown empty the second time I click on a button to show it.

Please, omit syntactic issues, I had to change some names due to my company policies so it cannot be identified and also translate from my language so there might be some syntactic issue but only from the translation. I also omitted Service and DAO classes because they are working fine, they return what I expect from them.

I work with Java 1.6, JSF 2.1 and Primefaces 6.0

So, I need help solving this issue or if someone knows a better approach, I am all eyes but in this case, please, try to make an extended answer because I really cannot think about any other way.

EDIT 1

I've removed Setter and Getter in order to make my code more clear for you, also removed some code assigning attributes to the Chart, but all these things are in my original code and working fine.

PollGraphicBean.java

@ManagedBean(name="pollGraphic")
@SessionScoped
public class GraficosEncuestaBean {

private LineChartModel graphicLine;

public GraficosEncuestaBean(){
    if(graphicLine == null){
        this.buildGraph();
    }
}


public void buildGraph(){
    HttpServletRequest parameterMap = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);

    String general = "";
    String clean = "";

    String date1 = "01-03-2017 00:00";
    String date2 = "30-04-2017 00:00";

    if(parameterMap != null){

        if(general != null){
            general = parameterMap.getParameter("general");
            session.setAttribute("hidden", general);
        }else if(clean != null){
            clean = parameterMap.getParameter("clean");
            session.setAttribute("hidden", clean);
        }

        this.buildGraphicFinal(date1,date2, (String) session.getAttribute("hidden"));


    }
}

private void buildGraphicFinal(String date1, String date2, String value){

    this.graphicLine = new PollGraphicService().getGraphicData(date1, date2, value);

}


}

chart_xxx.xhtml

 <ui:define>
    <p:panel>
        <h:form>
            <p:chart type="line" model="#{pollGraphic.graphicLine}" />
        </h:form>
    </p:panel>   
 </ui:define>

main_app.xhtml

 <ui:define>


    <p:panelGrid>
        <p:column>
            <p:panel>
                <div>
                    <h:form prependId="false">
                        <input type="hidden" value="General Rating" 
 name="general" id="general"/>
                        <p:commandButton value="Get results" 
 actionListener="#{pollGraphic.buildGraph}" onstart="location.href = 
 '/XXX/chart/chart_xxx.xhtml'"/>
                    </h:form>
                </div>
            </p:panel>
        </p:column>  
        <p:column>


                    <h:form prependId="false">
                        <input type="hidden" value="Clean Rating" 
  name="clean"/>
                        <p:commandButton value="Get results" 
  actionListener="#{pollGraphic.buildGraph}" onstart="location.href = 
  '/XXX/chart/chart_xxx.xhtml'"/>

                        </h:form>
                </div>
            </p:panel>
        </p:column>

    </p:panelGrid>
   </ui:define>
NeoChiri
  • 296
  • 4
  • 18
  • Why are you using the parameterMap this way and populating the session that way... Very oldschool... And trry reducing the code in your question. Way to much noise... [mcve] please – Kukeltje Apr 18 '18 at 13:30
  • I added all the necessary code that is involved in my problem, and I know it is not the best way to do it, but like I said, it is the only approach I was able to find due to the buildGraphic method being called several times, I am wide open to suggestions about how to manage it in a better way. – NeoChiri Apr 18 '18 at 13:38
  • No, much of the code is not related to the problem. All the divs, br style etc panelgrid etc irrelevant. Oh and prependId="false" should not be used.. http://stackoverflow.com/questions/7415230/uiform-with-prependid-false-breaks-fajax-render. And you should not do any work in a constructor... Not even lazy... Use the @PostConstruct annotation... – Kukeltje Apr 18 '18 at 13:42
  • I reformated my xhtml code erasing many of it, I cannot try now @PostConstruct and delete prependId, will do it tomorrow when I get to my office. – NeoChiri Apr 18 '18 at 14:12
  • Yes but only if you clear things up (you can clear up even more) we can see what the real usecase is and maybe where you go wrong... – Kukeltje Apr 18 '18 at 15:03
  • Ok, I cleared up and I don't know if there is something else to be cleared up, if so, please, tell me. – NeoChiri Apr 18 '18 at 19:19
  • @Kukeltje I tried with `@PostConstruct` and it solved my issue with the method being called several times but now it doesn't get the parameter I am sending; I want a parameter send to the Bean so it will get different data according to the parameter. – NeoChiri Apr 19 '18 at 06:52
  • Then use parameters the normal way. Pass it as a param in the method call – Kukeltje Apr 19 '18 at 07:47
  • And there are q/a about that https://stackoverflow.com/questions/13048930/how-to-pass-a-parameter-along-with-hcommandbutton – Kukeltje Apr 19 '18 at 07:58

1 Answers1

-1

Try changing @SessionScoped to @ViewScoped or even @RequestScoped

Siraj K
  • 158
  • 1
  • 8
  • I said in my comment I tried with `@RequestScoped` and it just doesn't show the chart and with `@ViewScoped` it throws a `NotSerializableException` – NeoChiri Apr 18 '18 at 12:36
  • Sorry I missed that comment. How are you navigating back in Step 2, 5? – Siraj K Apr 18 '18 at 12:46
  • I have a menu in the top bar, 2 menuItems with an action in them, on of them links to `main_app.xhtml` with faces-redirect=true, so that's what I use. – NeoChiri Apr 18 '18 at 12:50
  • BTW, your bean should implement `serializable`. – Siraj K Apr 18 '18 at 13:40
  • @SirajK: There are many other things 'wrong'... Looks like the code above contains many http://www.xyproblem.info related 'fixes'... – Kukeltje Apr 18 '18 at 13:43
  • @Kukeltje I now my approach is bad but like I said already, it’s the only thing that came to my mind so please, if you are willing to help and have a better idea, it is welcome but only saying that everything is wrong won’t help me at all, I explained what I want to do and pasted my approach and explained my issue too. – NeoChiri Apr 18 '18 at 14:13
  • @SirajK, I will do that. – NeoChiri Apr 18 '18 at 14:18
  • Why the downvote? I am certain that changing scope will fix the issue – Siraj K Apr 19 '18 at 12:40