如何在Javascript和JSF中使用数据库? [英] How use database in Javascript and JSF?

查看:163
本文介绍了如何在Javascript和JSF中使用数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图按照@BalusC的建议此处一>。
(我正在写这里,因为它与前面的问题无关)。

所以我需要从我的数据库中获取数据并使用JavaScript显示在图表中, 这是一个例子一>。
我只是做这个示例,所以我可以理解如何显示从服务器端到客户端的一些数据。



我的bean:

  @ManagedBean(name =reportc)
@ViewScoped
公共类ReportControl实现Serializable {
private static final long serialVersionUID = 3269125738504434502L;
$ b private String [] dataAsJson = {1.3,2.1,1.3,2.2,1.4,2.7,1.5,2.1,1.6 ,2.4,1.9,2.1};

public String getDataAsJson(){
Gson gson = new Gson();
返回gson.toJson(dataAsJson);


为了帮助理解 spline-plot-bands.js 文件。

 <!DOCTYPE html> 
< html lang =en
xmlns =http://www.w3.org/1999/xhtml
...

< H:头部>
< script type =text / javascriptsrc =http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js>< / script>

< h:outputScript> var data = $ {reportc.dataAsJson};< / h:outputScript>
< h:outputScript name =javascript / highchart / spline-plot-bands.js/>
< / h:头>
< h:body>

< h:outputScript name =javascript / highchart / highcharts.js/>
< h:outputScript name =javascript / highchart / modules / exporting.js/>

< div id =containerstyle =width:800px; height:400px; margin:0 auto>< / div>

< / h:body>
< / html>

正如您在 spline-plot-bands.js file。



对我来说重要的是这部分(我猜) p>

 系列:[{
名称:'Hestavollane',
data:[4.3,5.1,4.3,5.2 ,5.4,4.7,3.5,4.1,5.6,7.4,6.9,7.1,
7.9,7.9,7.5,6.7,7.7,7.7,7.4,7.0,7.1,5.8,5.9,7.4,
8.2 ,8.5,9.4,8.1,10.9,10.4,10.9,12.4,12.1,9.5,7.5,
7.1,7.5,8.1,6.8,3.4,2.1,1.9,2.8,2.9,1.3,4.4,4.2,... b $ b 3.0,3.0]

},{
名称:'Voll',
数据:[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 ,0.1,0.0,0.3,0.0,
0.0,0.4,0.0,0.1,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,
0.0,0.6,1.2,1.7,0.7 ,2.9,4.1,2.6,3.7,3.9,1.7,2.3,
3.0,3.3,4.8,5.0,4.8,5.0,3.2,2.0,0.9,0.4,0.3,0.5,0.4]
} ]

如何我可以发送这样的东西从我的服务器端到这个JavaScript?



我想我已经接近找出如何使用gson,javascript与jsf,但我仍然不知道如何完成这个。
有人可以帮我解决这个问题吗?

解决方案

JS需要一个 double [] ,但是您正在提供 String [] 。相应地修正:

  private double [] hestavollane = {
4.3,5.1, 4.3,5.2,5.4,4.7,3.5,4.1,5.6,7.4,6.9,7.1,
7.9,7.9,7.5,6.7,7.7,7.7,7.4,7.0,7.1,5.8,5.9,7.4,
8.2,8.5,9.4,8.1,10.9,10.4,10.9,12.4,12.1,9.5,7.5,
7.1,7.5,8.1,6.8,3.4,2.1,1.9,2.8,2.9,1.3,4.4, 4.2,
3.0,3.0
};

private double [] voll = {
0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.1,0.0,0.3,0.0,
0.0,0.4 ,0.0,0.1,0.0,0.0,0.0,0.0,0.0,0.0,0.0,
0.0,0.6,1.2,1.7,0.7,2.9,4.1,2.6,3.7,3.9,1.7,2.3,...,0.0,0.0, b $ b 3.0,3.3,4.8,5.0,4.8,5.0,3.2,2.0,0.9,0.4,0.3,0.5,0.4
};

public String getDataAsJson(){
Map< String,Object> data = new HashMap< String,Object>();
data.put(hestavollane,hestavollane);
data.put(voll,voll);
返回新的Gson()。toJson(data);
}

编辑您的 spline-plot-bands.js 文件使用它来代替硬编码的值:

 系列:[{
name :'Hestavollane',
data:data.hestavollane
},{
名称:'Voll',
data:data.voll
}]


I'm trying to follow the @BalusC advice here. (I'm writing here now because it's unrelated with previous question).

So I need to get data from my database and show in chart using JavaScript, this is an example. I'm just doing this sample so I can understand how to show some data from the server side to the client side.

My bean:

@ManagedBean(name="reportc")
@ViewScoped
public class ReportControl implements Serializable {
    private static final long serialVersionUID = 3269125738504434502L;

    private String[] dataAsJson = {"1.3", "2.1", "1.3", "2.2", "1.4", "2.7", "1.5", "2.1", "1.6", "2.4", "1.9", "2.1"};

    public String getDataAsJson() {
        Gson gson = new Gson();
        return  gson.toJson(dataAsJson);
    }
}

To help understand the spline-plot-bands.js file.

<!DOCTYPE html>
<html lang="en"
    xmlns="http://www.w3.org/1999/xhtml"
    ...

    <h:head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

        <h:outputScript>var data = ${reportc.dataAsJson};</h:outputScript>
        <h:outputScript name="javascript/highchart/spline-plot-bands.js" />
    </h:head>
    <h:body>

    <h:outputScript name="javascript/highchart/highcharts.js" />
    <h:outputScript name="javascript/highchart/modules/exporting.js" />

    <div id="container" style="width: 800px; height: 400px; margin: 0 auto"></div>

    </h:body>
</html>

As you can see in the spline-plot-bands.js file.

All that matters for me is this part (I guess):

series: [{
  name: 'Hestavollane',
  data: [4.3, 5.1, 4.3, 5.2, 5.4, 4.7, 3.5, 4.1, 5.6, 7.4, 6.9, 7.1,
         7.9, 7.9, 7.5, 6.7, 7.7, 7.7, 7.4, 7.0, 7.1, 5.8, 5.9, 7.4,
         8.2, 8.5, 9.4, 8.1, 10.9, 10.4, 10.9, 12.4, 12.1, 9.5, 7.5,
         7.1, 7.5, 8.1, 6.8, 3.4, 2.1, 1.9, 2.8, 2.9, 1.3, 4.4, 4.2,
         3.0, 3.0]

  }, {
  name: 'Voll',
  data: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.1, 0.0, 0.3, 0.0,
         0.0, 0.4, 0.0, 0.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
         0.0, 0.6, 1.2, 1.7, 0.7, 2.9, 4.1, 2.6, 3.7, 3.9, 1.7, 2.3,
         3.0, 3.3, 4.8, 5.0, 4.8, 5.0, 3.2, 2.0, 0.9, 0.4, 0.3, 0.5, 0.4]
  }]

How could I send something like this from my server side to this javascript ?

I think I'm close to find out how to use gson, javascript with jsf, but I still don't get it how to finish this. Could someone help me with this ?

解决方案

The JS expects a double[], but you're feeding a String[]. Fix it accordingly:

private double[] hestavollane = {
     4.3, 5.1, 4.3, 5.2, 5.4, 4.7, 3.5, 4.1, 5.6, 7.4, 6.9, 7.1,
     7.9, 7.9, 7.5, 6.7, 7.7, 7.7, 7.4, 7.0, 7.1, 5.8, 5.9, 7.4,
     8.2, 8.5, 9.4, 8.1, 10.9, 10.4, 10.9, 12.4, 12.1, 9.5, 7.5,
     7.1, 7.5, 8.1, 6.8, 3.4, 2.1, 1.9, 2.8, 2.9, 1.3, 4.4, 4.2,
     3.0, 3.0
};

private double[] voll = {
     0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.1, 0.0, 0.3, 0.0,
     0.0, 0.4, 0.0, 0.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
     0.0, 0.6, 1.2, 1.7, 0.7, 2.9, 4.1, 2.6, 3.7, 3.9, 1.7, 2.3,
     3.0, 3.3, 4.8, 5.0, 4.8, 5.0, 3.2, 2.0, 0.9, 0.4, 0.3, 0.5, 0.4
};

public String getDataAsJson() {
    Map<String, Object> data = new HashMap<String, Object>();
    data.put("hestavollane", hestavollane);
    data.put("voll", voll);
    return new Gson().toJson(data);
}

And edit your spline-plot-bands.js file to use it instead of the hardcoded values:

series: [{
    name: 'Hestavollane',
    data: data.hestavollane
}, {
    name: 'Voll',
    data: data.voll
}]

这篇关于如何在Javascript和JSF中使用数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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