java - spring mvc 3 和谷歌数据表

标签 java spring-mvc datatable datasource google-visualization

我是 google.visualization.datasource 的新手,我想知道是否有一个简单的示例可以使用辅助类创建数据表并将其返回给客户端,但是不使用查询和 DataSourceServlet?因为我已经通过 spring mvc Controller 实现了我的 http 请求。

我试图实现以下示例:https://developers.google.com/chart/interactive/docs/dev/dsl_get_started

这是我的代码:

    @Controller
public class DBcontroller {
private SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


@SuppressWarnings({ "rawtypes", "unchecked" })
@RequestMapping(value = "/getHistoricalData", method = {RequestMethod.POST,RequestMethod.GET})
public @ResponseBody DataTable setLampsRequest(@RequestParam String Lampid
        , @RequestParam String StartDate, @RequestParam String EndDate) throws ParseException{

    //converting time values to long
    Long start = formater.parse(StartDate).getTime();
    Long end = formater.parse(EndDate).getTime();

     // Create a data table,
    DataTable data = new DataTable();
    ArrayList cd = new ArrayList();
    cd.add(new ColumnDescription("name", ValueType.TEXT, "Animal name"));
    cd.add(new ColumnDescription("link", ValueType.TEXT, "Link to wikipedia"));
    cd.add(new ColumnDescription("population", ValueType.NUMBER, "Population size"));
    cd.add(new ColumnDescription("vegeterian", ValueType.BOOLEAN, "Vegetarian?"));

    data.addColumns(cd);

    // Fill the data table.
    try {
      data.addRowFromValues("Aye-aye", "http://en.wikipedia.org/wiki/Aye-aye", 100, true);
      data.addRowFromValues("Sloth", "http://en.wikipedia.org/wiki/Sloth", 300, true);
      data.addRowFromValues("Leopard", "http://en.wikipedia.org/wiki/Leopard", 50, false);
      data.addRowFromValues("Tiger", "http://en.wikipedia.org/wiki/Tiger", 80, false);
    } catch (TypeMismatchException e) {
      System.out.println("Invalid type!");
    }


    return data;
 }  

 }

提前感谢您的任何帮助。

最佳答案

您可以使用JsonRenderer将DataTable写入JSON:

@RequestMapping(value = "/getHistoricalData", method = {RequestMethod.POST,RequestMethod.GET})
public @ResponseBody void setLampsRequest(
            @RequestParam String Lampid, 
            @RequestParam String StartDate, 
            @RequestParam String EndDate, 
            Writer output, 
            HttpServletRequest request) {

    //converting time values to long
    Long start = formater.parse(StartDate).getTime();
    Long end = formater.parse(EndDate).getTime();

    // Create a data table,
    DataTable data = new DataTable();
    ArrayList cd = new ArrayList();
    cd.add(new ColumnDescription("name", ValueType.TEXT, "Animal name"));
    cd.add(new ColumnDescription("link", ValueType.TEXT, "Link to wikipedia"));
    cd.add(new ColumnDescription("population", ValueType.NUMBER, "Population size"));
    cd.add(new ColumnDescription("vegeterian", ValueType.BOOLEAN, "Vegetarian?"));

    data.addColumns(cd);

    // Fill the data table.
    try {
      data.addRowFromValues("Aye-aye", "http://en.wikipedia.org/wiki/Aye-aye", 100, true);
      data.addRowFromValues("Sloth", "http://en.wikipedia.org/wiki/Sloth", 300, true);
      data.addRowFromValues("Leopard", "http://en.wikipedia.org/wiki/Leopard", 50, false);
      data.addRowFromValues("Tiger", "http://en.wikipedia.org/wiki/Tiger", 80, false);
    } catch (TypeMismatchException e) {
      System.out.println("Invalid type!");
    }
    PrintWriter writer = new PrintWriter(output);
    writer.println(JsonRenderer.renderDataTable(data, true, false, false));
    writer.close();
}

关于java - spring mvc 3 和谷歌数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17163589/

相关文章:

java - 即使我使用 powermockito 来模拟类,也会出现 "Mockito cannot mock this class"错误

java - Doc 和 Docx 到 PDF 的转换,在 Linux 上使用 Java

java - Spring @valid 不起作用

asp.net - JQuery Datatable 插件通过 Ajax 调用加载 Asp.Net Repeater

javascript - 数据表排序不按降序显示

java - 是否同步到足以使 BlockingQueue 的 drainTo() 方法原子化?

java - 这段 XML 代码有什么问题?

java - Grails 应用程序使用来自 spring-mvc 后端的服务

java - 在 spring MVC 中将图像上传到服务器并将引用存储在 mysql 数据库中

jsf-2 - 如何从 ManagedBean 获取 primefaces 数据表列顺序和宽度