2013-03-18 49 views
4

我想從managedBean送我的ArrayList JavaScript代碼將數據發送到JavaScript和JSON,從豆在JSF

我的豆是在這裏:

public void getDataAsJson(){ 
    String [] dizi={"Tokyo","Jakarta","New York","Seoul", 
       "Manila","Mumbai","Sao Paulo","Mexico City", 
       "Dehli","Osaka","Cairo","Kolkata", 
       "Los Angeles","Shanghai","Moscow","Beijing", 
       "Buenos Aires","Guangzhou","Shenzhen","Istanbul"}; 

    Random rnd =new Random(); 

    JSONObject obj= new JSONObject(); 
    for (int i = 0; i < dizi.length; i++) 
     obj.put(dizi[i], new Integer(rnd.nextInt(80))); 
} 

我的javascript代碼是在這裏在XHTML頁面:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> 
<script type="text/javascript"> 
<!-- 

$(function() { 

    var chart; 
    $(document).ready(function() { 
     chart = new Highcharts.Chart({ 
      chart: { 
       renderTo: 'container', 
       zoomType: 'xy' 
      }, 
      title: { 
       text: 'avarage' 
      }, 
      subtitle: { 
       text: '' 
      }, 
      xAxis: [{ 
       gridLineWidth: 0.5, 
       categories: [// here is my city names which come from mybean] 
      }], 
      yAxis: [{ // Primary yAxis 
       labels: { 
        formatter: function() { 
         return this.value; 
        }, 
        style: { 
         color: '#89A54E' 
        } 
       }, 
       title: { 
        text: 'avarage', 
        style: { 
         color: '#89A54E' 
        } 
       } 
      }], 

      series: [{ 
       name: 'avarage', 
       color: '#89A54E', 
       type: 'spline', 
       data: [// // here is my city's avarage which come from mybean], 
         labels: { 
         rotation: -90, 
         align: 'right', 
         style: { 
          fontSize: '13px', 
          fontFamily: 'Verdana, sans-serif' 
         } 
        } 
      }] 
     }); 
    }); 
}); 
//--> 
</script> 

這是我在XHTML頁面主體:

<body> 
    <script src="http://code.highcharts.com/highcharts.js"></script> 
    <div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div> 
</body> 
+0

http://stackoverflow.com/editing-help#code – SLaks 2013-03-18 14:10:05

+0

那麼,你在此之後嘗試過什麼?我沒有看到任何JSF代碼嘗試調用你的'getDataAsJson'方法。 – 2013-03-18 14:25:35

+0

http://stackoverflow.com/a/9886212/617373 – Daniel 2013-03-18 14:38:56

回答

8

您需要理解JSF僅僅是一個HTML/JS代碼生成器。

您只需讓JSF 打印所需的數據,使其以語法上有效的JS代碼結束。

categories: #{bean.dataAsJson} 

其中getDataAsJson()返回String表示所期望的JSON代碼。例如。 基本上

public String getDataAsJson() { 
    return "['foo', 'bar', 'baz']"; 
} 

要驗證結果,右擊網頁瀏覽器,並做查看源

categories: ['foo', 'bar', 'baz']