Spring无法从DataTables获取绘制,开始和长度

时间:2018-08-25 14:37:37

标签: java spring spring-boot servlets datatables

我知道我做错了事,但是我找不到地方。如果您可以看一下我的代码并告诉我,为什么这些参数为null。我正在尝试将服务器端分页应用于我的应用程序和DataTables。 System.outs无效,因此我的“请求”错误或这些参数为null。

也许有人有关于分页等文章的有用链接?数据表中的手册对我没有帮助。

Spring Controller:

package com.example.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.dao.SeriaDao;
import com.example.model.Seria;


@RestController
public class WebController extends HttpServlet{

    @Autowired
    SeriaDao sed;


    @GetMapping("/tabelka")
    public List<Seria> showTable()
    {
        return sed.findAll();
    }

    int draw = 0;
    int start = 0;
    int length = 20;

    @GetMapping("/pgTabelka")
    public Map<String,Object> pgTable(HttpServletRequest request)
    {

        Map<String,Object> json = new HashMap<String,Object>();
        if(request.getParameter("draw")!=null)
        {
            draw = Integer.parseInt(request.getParameter("draw"));
            System.out.println("draw dziala");
        }
        if(request.getParameter("start")!=null)
        {
            start = Integer.parseInt(request.getParameter("start"));
            System.out.println("start dziala");
        }
        if(request.getParameter("length")!=null)
        {
            length = Integer.parseInt(request.getParameter("length"));
            System.out.println("length dziala");
        }
        int totalRecords = sed.recordsTotal();

        List<Seria> serie = sed.findPart(start, length);

            json.put("draw", draw);
            json.put("recordsTotal", totalRecords);
            json.put("recordsFiltered", totalRecords);
            json.put("data", serie);


        return json;
    }
}

HTML索引:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Data Viewer</title>
    <script
              src="https://code.jquery.com/jquery-3.3.1.js"
              integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
              crossorigin="anonymous"></script>
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.18/datatables.min.css"/>

    <script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.18/datatables.min.js"></script>

</head>
<body>

<table id="tab" class="display">
<thead>
    <tr>
        <th>id</th>
        <th>name</th>
        <th>file</th>
        <th>cassid</th>
        <th>categoryid</th>
        <th>datefrom</th>
        <th>frequency</th>
        <th>markers</th>
        <th>unit</th>
        <th>feed</th>
        <th>userid</th>
        <th>createdate</th>
        <th>changedate</th>
    </tr>
</thead>
</table>
<script>
    $('#tab').DataTable( {

        ajax: {
            processing: true,
            serverSide: true,
            url: '/pgTabelka',
        },
        columns: [
            {data: "id"},
            {data:"name"},
            {data:"file"},
            {data:"cassid"},
            {data:"categoryid"},
            {data:"datefrom"},
            {data:"frequency"},
            {data:"markers"},
            {data:"unit"},
            {data:"feed"},
            {data:"userid"},
            {data:"createdate"},
            {data:"changedate"}
        ]
    } );

    </script>
</body>
</html>

来自邮递员的JSON(简称):

{
"recordsFiltered": 488,
"data": [
{
"id": 41,
"name": "Average Weekly Earnings of All Employees: Total Private in Corpus Christi, TX (MSA)",
"file": "SMU48185800500000011.csv",
"cassid": "1d2e556b-031e-4c6f-aec4-981c4e907324",
"categoryid": 3,
"datefrom": "2006-12-31",
"frequency": 5,
"markers": null,
"unit": "$ per Week",
"feed": "Macroeconomic_And_Major_Markets",
"userid": null,
"createdate": "2016-10-15T23:38:49",
"changedate": "2017-01-09T00:50:01"
},
{
"id": 42,
"name": "Harmonized Index of Consumer Prices: Audio-Visual, Photographic, and Information Processing Equipment for Cyprus",
"file": "CP0910CYM086NEST.csv",
"cassid": "6df5b8d5-ec39-4860-930f-a8b355cce37a",
"categoryid": 3,
"datefrom": "1995-12-31",
"frequency": 5,
"markers": null,
"unit": "Index 2015=100",
"feed": "Macroeconomic_And_Major_Markets",
"userid": null,
"createdate": "2016-10-15T23:38:49",
"changedate": "2017-02-16T11:35:04"
},
{
........
}
],
"draw": 0,
"recordsTotal": 488
}

1 个答案:

答案 0 :(得分:0)

DataTables没有发送参数。

我意识到我在DataTables配置中犯了一个严重的错误:

错误

$('#tab').DataTable( {
    ajax: {
        processing: true,
        serverSide: true,
        url: '/pgTabelka',
    },
    columns: [
        {data: "id"},
        {data:"name"},
        {data:"file"},
        {data:"cassid"},
        {data:"categoryid"},
        {data:"datefrom"},
        {data:"frequency"},
        {data:"markers"},
        {data:"unit"},
        {data:"feed"},
        {data:"userid"},
        {data:"createdate"},
        {data:"changedate"}
    ]
} );

</script>

<script>
    $('#tab').DataTable( {

        processing: true,
        serverSide: true,
        ajax: {
            url: '/pgTabelka',
        },
        columns: [
            {data: "id"},
            {data:"name"},
            {data:"file"},
            {data:"cassid"},
            {data:"categoryid"},
            {data:"datefrom"},
            {data:"frequency"},
            {data:"markers"},
            {data:"unit"},
            {data:"feed"},
            {data:"userid"},
            {data:"createdate"},
            {data:"changedate"}
        ]
    } );

    </script>