有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

jsf更新java从Bean创建的Primefaces图表

我在bean中创建了一个饼图,并将其添加到面板中。现在我想从bean中更新这个图表,但是这个图表没有更新。我已经更新了模型,这是正确的,但为什么图表不是

下面是一幅图片,我的页面是什么样子的: enter image description here

当我按下按钮时,我会更新我的图表

以下是我创建图表的代码:

Panel p = (Panel) e.getComponent().getParent().getParent();
p.setStyle("width: 500px; height: 400px !important;");

p.getChildren().clear();

Application ap = facesContext.getApplication();
Chart chart = (Chart) ap.createComponent(facesContext, "org.primefaces.component.Chart",
        "org.primefaces.component.ChartRenderer");
chart.setId("chart" + chart.hashCode());

setTitle(p.getId(), msgs.get("dashboard_chart5_title"));

if (getMemberTrendStatsIsNotEmpty()) {
   HtmlForm form = (HtmlForm) ap.createComponent(HtmlForm.COMPONENT_TYPE);
   form.setId("memberTrendForm");

   chart.setType("bar");
   chart.setId("memberTrend");
   chart.setModel(memberTrendStats);
   ValueExpression ve = ap.getExpressionFactory().createValueExpression(facesContext.getELContext(),
                 "#{dashboardController.memberTrendStats}", PieChartModel.class);
   chart.setValueExpression("model", ve);
   form.getChildren().add(chart);

   OutputLabel out = (OutputLabel) ap.createComponent(OutputLabel.COMPONENT_TYPE);
   out.setFor("yearsBack");
   out.setValue(msgs.get("dashboard_chart1_search") + ": ");
   form.getChildren().add(out);

   InputText in = (InputText) ap.createComponent(InputText.COMPONENT_TYPE);
   in.setId("yearsBack");
   in.setValue(yearsBack);
   in.setStyle("margin-right:10px;");
   in.setType("number");

   NumberConverter nc = new NumberConverter();
   nc.setMaxFractionDigits(0);
   in.setConverter(nc);
   form.getChildren().add(in);

   CommandButton btn = (CommandButton) ap.createComponent(CommandButton.COMPONENT_TYPE);
   btn.setValue(msgs.get("dashboard_chart1_update"));

   AjaxBehavior ab = new AjaxBehavior();
   ExpressionFactory ef = facesContext.getApplication().getExpressionFactory();
   MethodExpression me = ef.createMethodExpression(facesContext.getELContext(),
       "#{dashboardController.updateMemberTrend}",
       null,
       new Class[] { ActionEvent.class });
   ab.setListener(me);
   btn.addClientBehavior("submit", ab);
   btn.setProcess("@form");
   btn.setAjax(true);

   btn.addActionListener(new MethodExpressionActionListener(me));

   form.getChildren().add(btn);

   p.getChildren().add(form);
} else {
   p.getChildren().add(getEmptyMessage());
}

下面是我刷新图表的代码:

msgs = langFacade.getLangProperty(facesContext.getViewRoot().getLocale().getLanguage());

InputText in = (InputText) findUIComponent("memberTrendForm:yearsBack");

if (in != null) {
    yearsBack = ((Long) in.getValue()).intValue();
}

createChartModels();

RequestContext.getCurrentInstance().update("memberTrendForm:memberTrend");

有人能帮我解决这个问题吗? Thx


共 (1) 个答案

  1. # 1 楼答案

    好的,我找到了解决方案,我在表达式中使用了错误的模型:

    ValueExpression ve = ap.getExpressionFactory().createValueExpression(facesContext.getELContext(),
                     "#{dashboardController.memberTrendStats}", BarChartModel.class);
    chart.setValueExpression("model", ve);