Java 在JSF中执行请求后未正确提供数据
在视图Java 在JSF中执行请求后未正确提供数据,java,jsf,primefaces,jsf-2,Java,Jsf,Primefaces,Jsf 2,在视图main\u app.xhtml中有5个不同的字段,每个字段都包含一个按钮,单击此按钮时,必须打开视图chart\u xxx.xhtml,它将显示一个包含与字段相关的不同数据的图表 i、 e: 一般评级将在图表中显示一些数据 清洁评级将在图表中显示一些不同的数据 等 当我点击按钮时,buildGraph()方法会被调用多次,通常是2次或3次,如果其中一些请求中显示了请求中发送的参数,而另一些则没有,我不知道为什么会发生这种情况,因此 我决定将请求发送的参数存储在会话中,但。。。我不知道为什
main\u app.xhtml
中有5个不同的字段,每个字段都包含一个按钮,单击此按钮时,必须打开视图chart\u xxx.xhtml
,它将显示一个包含与字段相关的不同数据的图表
i、 e:
一般评级将在图表中显示一些数据
清洁评级将在图表中显示一些不同的数据
等
当我点击按钮时,buildGraph()
方法会被调用多次,通常是2次或3次,如果其中一些请求中显示了请求中发送的参数,而另一些则没有,我不知道为什么会发生这种情况,因此
我决定将请求发送的参数存储在会话中,但。。。我不知道为什么,如果我返回到main\u app.xhtml
,单击另一个按钮,视图chart\u xxx.xhtml
将显示先前发送的请求中的数据,
但是如果我再次返回到main\u app.xhtml
并再次单击同一按钮,则会显示正确的数据。
例如:
main\u app.xhtml
main\u app.xhtml
@RequestScoped
,则当我第二次单击按钮显示图表时,图表将显示为空
请忽略语法问题,由于我的公司政策,我不得不更改一些名称,因此无法识别,也无法从我的语言进行翻译,因此可能存在一些语法问题,但只能从翻译中进行。
我还省略了服务类和DAO类,因为它们工作得很好,它们返回我所期望的结果
我使用Java1.6、JSF2.1和Primefaces 6.0
所以,我需要帮助解决这个问题,或者如果有人知道一个更好的方法,我会全神贯注,但在这种情况下,请尝试给出一个扩展的答案,因为我真的无法思考其他方法
编辑1
为了让我的代码更清晰,我删除了Setter和Getter,还删除了一些为图表分配属性的代码,但所有这些都在我的原始代码中,工作正常
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);
}
}
图表xxx.xhtml
<ui:define>
<p:panel>
<h:form>
<p:chart type="line" model="#{pollGraphic.graphicLine}" />
</h:form>
</p:panel>
</ui:define>
<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>
main_app.xhtml
<ui:define>
<p:panel>
<h:form>
<p:chart type="line" model="#{pollGraphic.graphicLine}" />
</h:form>
</p:panel>
</ui:define>
<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
我在评论中说,我尝试了@RequestScoped
,但它没有显示图表,而且@ViewScoped
会引发NotSerializableException
很抱歉,我错过了那条评论。在第2步、第5步中,你是如何返回的?我在顶部栏中有一个菜单,两个菜单项中有一个操作,其中一个链接指向main\u app.xhtml
,faces redirect=true,所以这就是我使用的。顺便说一句,你的bean应该实现serializable
@SirajK:还有很多其他“错误”。。。看起来上面的代码包含许多相关的“修复”…为什么要这样使用parameterMap并以这种方式填充会话。。。非常古老的学校。。。并尝试减少问题中的代码。太吵了。。。请添加我的问题所涉及的所有必要代码,我知道这不是最好的方法,但正如我所说的,这是我能够找到的唯一方法,因为buildGraphic方法被多次调用,我对如何更好地管理它的建议持开放态度。不,大部分代码与问题无关。所有的div、br样式等都不相关。Oh和prependId=“false”不应使用。你不应该在构造函数中做任何工作。。。甚至不懒惰。。。使用@PostConstruct注释…我重新格式化了我的xhtml代码,删除了很多内容,我现在无法尝试@PostConstruct并删除prependId,明天我到办公室时会这样做。是的,但只有你把事情弄清楚(你可以更清楚),我们才能看到真正的用例是什么,也许你会出错。。。