在 JSF 中执行请求后,未正确提供数据



在视图中main_app.xhtml有 5 个不同的字段,每个字段都包含一个按钮,单击此按钮时,它必须打开视图chart_xxx.xhtml,它将显示一个图表,其中包含与该字段相关的不同数据 即:

一般评级将在图表中显示一些数据 干净评级将在图表中显示一些不同的数据 等

当我单击该按钮时,buildGraph()方法被调用了几次,通常是 2 或 3 次,其中一些请求会显示请求中发送的参数,而其他请求则没有,我不知道为什么会发生这种情况,因此 我决定将请求发送的参数存储在会话中,但是...我不知道为什么,如果我回到main_app.xhtml,单击不同的按钮,视图chart_xxx.xhtml将显示上一个发送的请求中的数据, 但是,如果我再次返回main_app.xhtml并在同一按钮中再次单击,则会显示正确的数据。 例如:

  1. 我点击一般评级按钮,其数据显示在图表上。
  2. 回到main_app.xhtml
  3. 点击清洁评级
  4. 显示来自一般评级的数据。
  5. 返回main_app.xhtml
  6. 再次点击清洁评级
  7. 清洁评级的数据正确显示

我已经多次阅读了这个主题为什么 JSF 多次调用 getter,但我找不到一种方法来适应我的功能。 此外,如果我使用@RequestScoped,当我第二次单击按钮显示它时,图表显示为空。

请省略句法问题,由于我的公司政策,我不得不更改一些名称,因此无法识别它,并且还从我的语言翻译,因此可能存在一些句法问题,但仅来自翻译。 我还省略了服务和 DAO 类,因为它们工作正常,它们返回了我对它们的期望。

我使用 Java 1.6、JSF 2.1 和 Primefaces 6.0

所以,我需要帮助解决这个问题,或者如果有人知道更好的方法,我都是眼睛,但在这种情况下,请尝试做出扩展的答案,因为我真的无法考虑任何其他方法。

编辑 1

我删除了 Setter 和 Getter 以使我的代码对您更清晰,还删除了一些为图表分配属性的代码,但所有这些内容都在我的原始代码中并且工作正常。

投票图豆.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>

尝试将@SessionScoped更改为@ViewScoped甚至@RequestScoped

最新更新