DataTables与Spring MVC、Spring Data JPA的集成

1、如何接收参数,这个代码是参考老外的代码。不过核心逻辑老外写的太差了。

public class DataTablesInput {

/**
* Draw counter. This is used by DataTables to ensure that the Ajax returns
* from server-side processing requests are drawn in sequence by DataTables
* (Ajax requests are asynchronous and thus can return out of sequence).
* This is used as part of the draw return parameter (see below).
*/
@NotNull
@Min(0)
private Integer draw;

/**
* Paging first record indicator. This is the start point in the current
* data set (0 index based - i.e. 0 is the first record).
*/
@NotNull
@Min(0)
private Integer start;

/**
* Number of records that the table can display in the current draw. It is
* expected that the number of records returned will be equal to this
* number, unless the server has fewer records to return. Note that this can
* be -1 to indicate that all records should be returned (although that
* negates any benefits of server-side processing!)
*/
@NotNull
@Min(-1)
private Integer length;

/**
* Global search parameter.
*/
@NotNull
private SearchParameter search;

/**
* Order parameter
*/
@NotEmpty
private List<OrderParameter> order;

/**
* Per-column search parameter
*/
@NotEmpty
private List<ColumnParameter> columns;

public DataTablesInput() {
this.search = new SearchParameter();
this.order = new ArrayList<OrderParameter>();
this.columns = new ArrayList<ColumnParameter>();
}

public Integer getDraw() {
return draw;
}

public void setDraw(Integer draw) {
this.draw = draw;
}

public Integer getStart() {
return start;
}

public void setStart(Integer start) {
this.start = start;
}

public Integer getLength() {
return length;
}

public void setLength(Integer length) {
this.length = length;
}

public SearchParameter getSearch() {
return search;
}

public void setSearch(SearchParameter search) {
this.search = search;
}

public List<OrderParameter> getOrder() {
return order;
}

public void setOrder(List<OrderParameter> order) {
this.order = order;
}

public List<ColumnParameter> getColumns() {
return columns;
}

public void setColumns(List<ColumnParameter> columns) {
this.columns = columns;
}

public List<ColumnParameter> addColumn(ColumnParameter column) {
if(Collections3.isEmpty(columns)){
columns = new ArrayList<ColumnParameter>();
}
columns.add(column);
return columns;
}
}


2、如何输出

public class DataTablesOutput<T> {

/**
* The draw counter that this object is a response to - from the draw
* parameter sent as part of the data request. Note that it is strongly
* recommended for security reasons that you cast this parameter to an
* integer, rather than simply echoing back to the client what it sent in
* the draw parameter, in order to prevent Cross Site Scripting (XSS)
* attacks.
*/
@JsonView(View.class)
private Integer draw;

/**
* Total records, before filtering (i.e. the total number of records in the
* database)
*/
@JsonView(View.class)
private Long recordsTotal;

/**
* Total records, after filtering (i.e. the total number of records after
* filtering has been applied - not just the number of records being
* returned for this page of data).
*/
@JsonView(View.class)
private Long recordsFiltered;

/**
* The data to be displayed in the table. This is an array of data source
* objects, one for each row, which will be used by DataTables. Note that
* this parameter's name can be changed using the ajaxDT option's dataSrc
* property.
*/
@JsonView(View.class)
private List<Map<String, String>> data;

private List<T> sourceData;

/**
* Optional: If an error occurs during the running of the server-side
* processing script, you can inform the user of this error by passing back
* the error message to be displayed using this parameter. Do not include if
* there is no error.
*/
@JsonView(View.class)
private String error;

public interface View {
}

public Integer getDraw() {
return draw;
}

public void setDraw(Integer draw) {
this.draw = draw;
}

public Long getRecordsTotal() {
return recordsTotal;
}

public void setRecordsTotal(Long recordsTotal) {
this.recordsTotal = recordsTotal;
}

public Long getRecordsFiltered() {
return recordsFiltered;
}

public void setRecordsFiltered(Long recordsFiltered) {
this.recordsFiltered = recordsFiltered;
}

public List<Map<String, String>> getData() {
return data;
}

public void setData(List<Map<String, String>> data) {
this.data = data;
}

public List<T> getSourceData() {
return sourceData;
}

public void setSourceData(List<T> sourceData) {
this.sourceData = sourceData;
}

public String getError() {
return error;
}

public void setError(String error) {
this.error = error;
}

}


3、中间过程,通过另外一个查询的参数Searchable,可以百度‘开涛的博客’,将DataTablesInput传来的参数赋值到searchable来,包括分页和排序等,放入Service查询出来之后,再格局DataTablesInput的columns中表单需要展现的字段取出,最后封装到DataTableOutput中去。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值