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:
- I click on General Rating button, its data is shown on the chart.
- Go back to
main_app.xhtml
- Click on Clean rating
- Data from General Rating is shown.
- Go back to
main_app.xhtml
- Click on Clean rating again
- 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>