如何从jQuery调用backing bean方法?

时间:2014-09-13 08:30:28

标签: jquery jsf

我想在我的jsf项目中使用jquery图表来做一些统计,如何在jquery函数中调用Java方法(来自managedbesn)? 这是我的jquery函数来显示条形图

var randomScalingFactor = function(){return Math.round(Math.random()* 100)};

var barChartData = {
    labels : ["January","February","March","April","May","June","July"],
    datasets : [
        {
            fillColor : "rgba(220,220,220,0.5)",
            strokeColor : "rgba(220,220,220,0.8)",
            highlightFill: "rgba(220,220,220,0.75)",
            highlightStroke: "rgba(220,220,220,1)",
            data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
        },
        {
            fillColor : "rgba(151,187,205,0.5)",
            strokeColor : "rgba(151,187,205,0.8)",
            highlightFill : "rgba(151,187,205,0.75)",
            highlightStroke : "rgba(151,187,205,1)",
            data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
        }
    ]

}
window.onload = function(){
    var ctx = document.getElementById("canvas").getContext("2d");
    window.myBar = new Chart(ctx).Bar(barChartData, {
        responsive : true
    });
}

我想用java方法调用数据来填写字段标签(标签:[“January”,“February”,“March”,“April”,“May”,“June”,“July”])例如:

公开列表getmonths(){-------- code -------}

1 个答案:

答案 0 :(得分:0)

在托管bean中

  List<String> months = new ArrayList<String>();
  public List<String> getMonths(){
    if(months.isEmpty()) {
      months.add("January");
      months.add("February");
      months.add("March");
      months.add("April");
    }
    return months;
  }

然后在xhtml页面中访问以下列表。

var barChartData = {labels : ["#{MyBean.months.get(0)}","#{MyBean.months.get(1)}","#{MyBean.months.get(2)}","#{MyBean.months.get(3)}"]}