在jsf中使用json将数据从bean发送到javascript [英] send data from bean to javascript with json in jsf

查看:85
本文介绍了在jsf中使用json将数据从bean发送到javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的arraylist从managedBean发送到javascript代码,

I want to send my arraylist from managedBean to javascript code,

我的豆在这里:

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页面中:

my javascript code is here in xhtml page:

<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页面中的尸体:

here is my body in xhtml page:

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

推荐答案

您需要了解,在此问题的上下文中,JSF仅是HTML/JS代码生成器.

You need to understand that JSF is in the context of this question merely a HTML/JS code generator.

您只需要让JSF 打印所需的数据,使其最终成为语法上有效的JS代码即可.

You just need to let JSF print the desired data in such way that it ends up in syntactically valid JS code.

categories: #{bean.dataAsJson}

其中,getDataAsJson()返回表示所需JSON代码的String.例如. 基本上:

Wherein getDataAsJson() returns a String representing the desired JSON code. E.g. basically:

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

要验证结果,请在浏览器中右键单击页面,然后执行查看源代码.

To verify the result, rightclick page in browser and do View Source.

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

这篇关于在jsf中使用json将数据从bean发送到javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆