在Spring Boot的服务器端使用DataTables

问题描述:

我在Java Spring Boot项目中使用jQuery DataTables.使用DataTables的服务器端处理时,它会发送AJAX请求,并带有以下请求参数:

I'm using jQuery DataTables in a Java Spring Boot project. When using DataTables's server-side processing, it sends AJAX request with request parameters like:

?columns[0][data]=0
&columns[0][name]=name
&columns[0][searchable]=true
&columns[0][orderable]=true
&columns[0][search][value]=Tom
&columns[0][search][regex]=false
&columns[1][data]=1
&columns[1][name]=address
&columns[1][searchable]=true
&columns[1][orderable]=true
&columns[1][search][value]=
&columns[1][search][regex]=false

到我的服务器.

如何将这些请求参数转换为Java对象以进行处理?本教程只是指出

How can I convert these request parameters to a Java object for processing? The tutorial simply states that

在大多数现代服务器端脚本环境中,这些数据将作为数组自动提供给您.

In most modern server-side scripting environments this data will automatically be available to you as an array.

但是我找不到在Java中执行此操作的任何方法,特别是使用Spring Boot的@RequestParameter.

but I cannot find any way to do this in Java, particularly using Spring Boot's @RequestParameter.

谢谢您的帮助!

创建以下类,忽略程序包名称 //DataTableRequest.java

Create the following classes, ignore the package names //DataTableRequest.java

package com.employee.app.model;

import java.util.*;
import com.fasterxml.jackson.annotation.*;

public class DataTableRequest {
private String draw;
private List<Column> columns;
private List<Order> order;
private String start;
private String length;
private Search search;
private String empty;

@JsonProperty("draw")
public String getDraw() { return draw; }
@JsonProperty("draw")
public void setDraw(String value) { this.draw = value; }

@JsonProperty("columns")
public List<Column> getColumns() { return columns; }
@JsonProperty("columns")
public void setColumns(List<Column> value) { this.columns = value; }

@JsonProperty("order")
public List<Order> getOrder() { return order; }
@JsonProperty("order")
public void setOrder(List<Order> value) { this.order = value; }

@JsonProperty("start")
public String getStart() { return start; }
@JsonProperty("start")
public void setStart(String value) { this.start = value; }

@JsonProperty("length")
public String getLength() { return length; }
@JsonProperty("length")
public void setLength(String value) { this.length = value; }

@JsonProperty("search")
public Search getSearch() { return search; }
@JsonProperty("search")
public void setSearch(Search value) { this.search = value; }

@JsonProperty("_")
public String getEmpty() { return empty; }
@JsonProperty("_")
public void setEmpty(String value) { this.empty = value; }
}

//Column.java

// Column.java

package com.employee.app.model;

import java.util.*;
import com.fasterxml.jackson.annotation.*;

public class Column {
private String data;
private String name;
private String searchable;
private String orderable;
private Search search;

@JsonProperty("data")
public String getData() { return data; }
@JsonProperty("data")
public void setData(String value) { this.data = value; }

@JsonProperty("name")
public String getName() { return name; }
@JsonProperty("name")
public void setName(String value) { this.name = value; }

@JsonProperty("searchable")
public String getSearchable() { return searchable; }
@JsonProperty("searchable")
public void setSearchable(String value) { this.searchable = value; }

@JsonProperty("orderable")
public String getOrderable() { return orderable; }
@JsonProperty("orderable")
public void setOrderable(String value) { this.orderable = value; }

@JsonProperty("search")
public Search getSearch() { return search; }
@JsonProperty("search")
public void setSearch(Search value) { this.search = value; }
}

//Search.java

// Search.java

package com.employee.app.model;

import java.util.*;
import com.fasterxml.jackson.annotation.*;

public class Search {
private String value;
private String regex;

@JsonProperty("value")
public String getValue() { return value; }
@JsonProperty("value")
public void setValue(String value) { this.value = value; }

@JsonProperty("regex")
public String getRegex() { return regex; }
@JsonProperty("regex")
public void setRegex(String value) { this.regex = value; }
}

//Order.java

// Order.java

package com.employee.app.model;

import java.util.*;
import com.fasterxml.jackson.annotation.*;

public class Order {
private String column;
private String dir;

@JsonProperty("column")
public String getColumn() { return column; }
@JsonProperty("column")
public void setColumn(String value) { this.column = value; }

@JsonProperty("dir")
public String getDir() { return dir; }
@JsonProperty("dir")
public void setDir(String value) { this.dir = value; }
}

默认情况下,DataTables以FormData的形式发送请求,以使其以Json的形式发送该请求,

DataTables by default sends requests as FormData, to make it send that request as Json, do the following.

$(document).ready(function() {
    $('#datatableId').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax":{
        url: "your_processing_endpoint",
        type:"POST",
        contentType:"application/json",
        data:function(d){
        return JSON.stringify(d)
        }
        },
//include other options
    } );
} );

然后在控制器操作中,假设您使用的是Spring boot,请执行以下操作

And then in the controller action, assuming your are using Spring boot, do the following

@RequestMapping(value="your_processing_endpoint",method="RequestMethod.POST")
public ResponseEntity<?> processDataTableRequest(@RequestBody DataTableRequest 
datatableRequest){
//you can add your logic here 
}