将java变量传递给javascript函数

时间:2013-10-17 07:16:29

标签: javascript jsf jsf-2 primefaces

我需要将java变量传递给javascript函数。我的代码如下。想知道这是正确的方法吗?

因为我在首次加载页面时遇到问题,并且单击了该按钮,所以chartData和categories为空。

我期待以下内容:

chartData = [{"name":"Anne","data":[1.0,0.0,4.0]},
             {"name":"Billy","data":[5.0,7.0,10000.0]}]
categories = ["APPLES","BANANAS","ORANGES"]

public String doLoadChartDataAction () {

    String _cat = "[{\"name\":\"Anne\",\"data\":[1.0,0.0,4.0]},{\"name\":\"Billy\",\"data\":[5.0,7.0,10000.0]}]";
    String _data = "[\"APPLES\",\"BANANAS\",\"ORANGES\"]";

    System.out.println("1.0 " + " _cat:" + _cat);
    System.out.println("1.0 " + " _data:" + _data);

    setCategories(_cat);
    setChartData(_data);

    return "";      
}


 <p:commandButton styleClass="commandButton" value="This" id="btnThis" action="#{pc_Test.doLoadChartDataAction}" 
    oncomplete="renderChart('container','line','Sample Chart','${pc_Test.chartData}', '${pc_Test.categories}');">   
 </p:commandButton>

1 个答案:

答案 0 :(得分:1)

public String doLoadChartDataAction () {

String _cat = "[{\"name\":\"Anne\",\"data\":[1.0,0.0,4.0]},{\"name\":\"Billy\",\"data\":[5.0,7.0,10000.0]}]";
String _data = "[\"APPLES\",\"BANANAS\",\"ORANGES\"]";

System.out.println("1.0 " + " _cat:" + _cat);
System.out.println("1.0 " + " _data:" + _data);

//setCategories(_cat);
//setChartData(_data);

RequestContext context = RequestContext.getCurrentInstance();
context.addCallbackParam("chartData", _data);
context.addCallbackParam("categories", _cat);

return "";      
}

<p:commandButton styleClass="commandButton" value="This" id="btnThis" action="#{pc_Test.doLoadChartDataAction}" 
oncomplete="renderChart('container','line','Sample Chart', args.chartData, args.categories);">