Struts2+DataTables动态生成数据表

Server-side processing

  • 新建返回参数变量、部分发送参数变量,以及set和get方法
  • 在struts的action方法中,根据传入的发送参数,生成返回参数变量值
    image

Html Datatables JS代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<script type="text/javascript">
$(document).ready(function () {
$("#companies").dataTable({
"processing": true,
"serverSide": true,
"ajax": "selectOptionAjaxRequest.action",
"columnDefs": [ { orderable: true, targets: [0,1,2] }],
"sPaginationType": "full_numbers",
"bFilter": false,
"columns": [
{ "data": "name" },
{ "data": "address" },
{ "data": "town" }
]
});
});
</script>

Struts Action Java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.ibm.cxl.action;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import jquery.datatables.model.Company;
import jquery.datatables.model.DataRepository;
public class RptConfig01Action extends ActionSupport {
private static final long serialVersionUID = -7160474110317441632L;
private Integer draw=0, recordsTotal=1, recordsFiltered = 0, length = 0, start=0;
private List<Company> data=new ArrayList<Company>();
public Integer getDraw() {
return draw;
}
public void setDraw(Integer draw) {
this.draw = draw;
}
public Integer getRecordsTotal() {
return recordsTotal;
}
public void setRecordsTotal(Integer recordsTotal) {
this.recordsTotal = recordsTotal;
}
public Integer getRecordsFiltered() {
return recordsFiltered;
}
public void setRecordsFiltered(Integer recordsFiltered) {
this.recordsFiltered = recordsFiltered;
}
public Integer getLength() {
return length;
}
public void setLength(Integer length) {
this.length = length;
}
public Integer getStart() {
return start;
}
public void setStart(Integer start) {
this.start = start;
}
@SuppressWarnings("unchecked")
public String execute() throws Exception {
recordsTotal=DataRepository.GetCompanies().size();
recordsFiltered = recordsTotal;
//print all parameters from the action context
Map<String, Object> map = ActionContext.getContext().getParameters();
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
System.out.println("key:"+ key +" value: " + Arrays.toString((String[])value));
}
final int sortColumnIndex = Integer.parseInt(((String[]) map.get("order[0][column]"))[0]);
final int sortDirection = ((String[]) map.get("order[0][dir]"))[0].equals("asc") ? -1 : 1;
for(Company c : DataRepository.GetCompanies()){
data.add(c);
}
Collections.sort(data, new Comparator<Company>(){
@Override
public int compare(Company c1, Company c2) {
switch(sortColumnIndex){
case 0:
return c1.getName().compareTo(c2.getName()) * sortDirection;
case 1:
return c1.getAddress().compareTo(c2.getAddress()) * sortDirection;
case 2:
return c1.getTown().compareTo(c2.getTown()) * sortDirection;
}
return 0;
}
});
if(recordsTotal< start + length) {
data = data.subList(start, length);
} else {
data = data.subList(start, start + length);
}
return SUCCESS;
}
public List<Company> getData() {
return data;
}
public void setData(List<Company> data) {
this.data = data;
}
}
Top