【问题标题】:How to call backing bean method from jQuery?如何从 jQuery 调用支持 bean 方法?
【发布时间】:2014-09-15 18:56:29
【问题描述】:

我想在我的 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方法调用的数据填写字段标签(标签:[“一月”,“二月”,“三月”,“四月”,“五月”,“六月”,“七月”])例如:

public list getmonths(){--------code-------}

【问题讨论】:

  • 发布您尝试过的内容,以便我们了解您的想法。
  • 当然它可能包括 ajax
  • 但我不明白这样做有什么问题。 @ArunPJohny
  • 很多初学者都没有意识到 JSF 在这类问题的上下文中仅仅是一个 HTML/CSS/JS 代码生成器。因此,您所要做的就是编写 JSF 代码,使其准确生成所需的 JS 输出。例如。 <script>var foo = '#{bean.foo}';</script>。你知道吗,比尔?如果是这样,那么请重新构建问题以提出更具体的问题并详细说明为什么这种方法不合适。
  • 好方法 BalusC thxs :)

标签: jquery jsf


【解决方案1】:

在你的豆子里

  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)}"]}

【讨论】:

    猜你喜欢
    • 2012-11-27
    • 1970-01-01
    • 2014-02-16
    • 1970-01-01
    • 2011-04-04
    • 1970-01-01
    • 2012-09-15
    • 2011-03-13
    • 2012-11-18
    相关资源
    最近更新 更多