找了两个例子但是都是spring,没有jfinal求大神指点:
https://github.com/rakurakupg/springmvc-jquerydatatables-example
https://github.com/sleepiejohn/spring-data-datatables-integration
补充:
写了一个方法实现了datatbales转javabean,通过jfianl的getKv获取所有params然后遍历去处理!谢谢@jfinal帮助 !
public static DataSrc getDatatablesJavaBean(Kv k) { Set<String> keySet = k.keySet(); DataSrc dataSrc = new DataSrc(); for (String key : keySet) { if (key.contains("columns")) { int i = Integer.parseInt(key.substring(key.indexOf("[")+1, key.indexOf("]"))); Column column = null; if (dataSrc.getColumns().containsKey(i)) { column = dataSrc.getColumns().get(i); }else { column = new Column(); } if (key.contains("[data]")) { column.setData(k.getStr(key)); }else if(key.contains("[name]")) { column.setName(k.getStr(key)); }else if(key.contains("[searchable]")) { column.setSearchable(Boolean.parseBoolean(k.getStr(key))); }else if(key.contains("[orderable]")) { column.setOrderable(Boolean.parseBoolean(k.getStr(key))); }else if(key.contains("[search][value]")) { column.getSearch().setValue(k.getStr(key)); }else if(key.contains("[search][regex]") ) { /*System.out.println("key [search][regex] :" + key); System.out.println("key [search][regex] :" + k.getStr(key)); System.out.println("column.getSearch(): " + column.getSearch());*/ column.getSearch().setRegex(Boolean.parseBoolean(k.getStr(key))); } dataSrc.getColumns().put(i, column); }else if (key.contains("order")) { int i = Integer.parseInt(key.substring(key.indexOf("[")+1, key.indexOf("]"))); Order order = null; if (dataSrc.getOrder().containsKey(i)) { order = dataSrc.getOrder().get(i); }else { order = new Order(); } if (key.contains("[column]")) { order.setColumn(Integer.valueOf(k.getStr(key))); }else if(key.contains("[dir]")) { order.setDir(k.getStr(key)); } dataSrc.getOrder().put(i, order); }else if (key.equals("start")) { dataSrc.setStart(Integer.valueOf(k.getStr(key))); }else if (key.equals("length")) { dataSrc.setLength(Integer.valueOf(k.getStr(key))); }else if (key.equals("search[value]")) { dataSrc.getSearch().setValue((k.getStr(key))); }else if (key.equals("search[regex]")) { dataSrc.getSearch().setRegex(Boolean.parseBoolean(k.getStr(key))); } } return dataSrc; }
Controller中调用下面的方法无效:
DataSrc dataSrc = getBean(DataSrc.class);
System.out.println(dataSrc.getColumns().size());
无效
前端提交的数据:
draw: 4 columns[0][data]: columns[0][name]: columns[0][searchable]: true columns[0][orderable]: true columns[0][search][value]: columns[0][search][regex]: false columns[1][data]: function columns[1][name]: columns[1][searchable]: true columns[1][orderable]: true columns[1][search][value]: columns[1][search][regex]: false columns[2][data]: bianhao columns[2][name]: columns[2][searchable]: true columns[2][orderable]: true columns[2][search][value]: columns[2][search][regex]: false columns[3][data]: changjia columns[3][name]: columns[3][searchable]: true columns[3][orderable]: true columns[3][search][value]: columns[3][search][regex]: false columns[4][data]: pinzhong columns[4][name]: columns[4][searchable]: true columns[4][orderable]: true columns[4][search][value]: columns[4][search][regex]: false columns[5][data]: cangku columns[5][name]: columns[5][searchable]: true columns[5][orderable]: true columns[5][search][value]: columns[5][search][regex]: false columns[6][data]: caizhi columns[6][name]: columns[6][searchable]: true columns[6][orderable]: true columns[6][search][value]: columns[6][search][regex]: false columns[7][data]: guige columns[7][name]: columns[7][searchable]: true columns[7][orderable]: true columns[7][search][value]: columns[7][search][regex]: false columns[8][data]: zhongliang columns[8][name]: columns[8][searchable]: true columns[8][orderable]: true columns[8][search][value]: columns[8][search][regex]: false columns[9][data]: weizhi columns[9][name]: columns[9][searchable]: true columns[9][orderable]: true columns[9][search][value]: columns[9][search][regex]: false columns[10][data]: beizhu columns[10][name]: columns[10][searchable]: true columns[10][orderable]: true columns[10][search][value]: columns[10][search][regex]: false columns[11][data]: function columns[11][name]: columns[11][searchable]: true columns[11][orderable]: true columns[11][search][value]: columns[11][search][regex]: false columns[12][data]: function columns[12][name]: columns[12][searchable]: true columns[12][orderable]: true columns[12][search][value]: columns[12][search][regex]: false columns[13][data]: liubanjieru columns[13][name]: columns[13][searchable]: true columns[13][orderable]: true columns[13][search][value]: columns[13][search][regex]: false columns[14][data]: function columns[14][name]: columns[14][searchable]: true columns[14][orderable]: true columns[14][search][value]: columns[14][search][regex]: false columns[15][data]: LB_time columns[15][name]: columns[15][searchable]: true columns[15][orderable]: true columns[15][search][value]: columns[15][search][regex]: false columns[16][data]: cancelUserName columns[16][name]: columns[16][searchable]: true columns[16][orderable]: true columns[16][search][value]: columns[16][search][regex]: false columns[17][data]: cancelTime columns[17][name]: columns[17][searchable]: true columns[17][orderable]: true columns[17][search][value]: columns[17][search][regex]: false columns[18][data]: stockOutTime columns[18][name]: columns[18][searchable]: true columns[18][orderable]: true columns[18][search][value]: columns[18][search][regex]: false columns[19][data]: saleTime columns[19][name]: columns[19][searchable]: true columns[19][orderable]: true columns[19][search][value]: columns[19][search][regex]: false order[0][column]: 11 order[0][dir]: asc start: 0 length: 500 search[value]: search[regex]: false
后端写的javaBean:
package com.steel.datatables; import java.util.List; public class DataSrc { private List<Column> columns; private List<Order> order; private Integer start; private Integer length; private Search search; public List<Column> getColumns() { return columns; } public void setColumns(List<Column> columns) { this.columns = columns; } public List<Order> getOrder() { return order; } public void setOrder(List<Order> order) { this.order = order; } 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 Search getSearch() { return search; } public void setSearch(Search search) { this.search = search; } }
Column:
package com.steel.datatables; public class Column { private String data; private String name; private boolean searchable; private boolean orderable; private Search search; public String getData() { return data; } public void setData(String data) { this.data = data; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isSearchable() { return searchable; } public void setSearchable(boolean searchable) { this.searchable = searchable; } public boolean isOrderable() { return orderable; } public void setOrderable(boolean orderable) { this.orderable = orderable; } public Search getSearch() { return search; } public void setSearch(Search search) { this.search = search; } }
order:
package com.steel.datatables; public class Order { private Integer column; private String dir; public Integer getColumn() { return column; } public void setColumn(Integer column) { this.column = column; } public String getDir() { return dir; } public void setDir(String dir) { this.dir = dir; } }
search:
package com.steel.datatables; public class Search { private String value; private boolean regex; public String getValue() { return value; } public void setValue(String value) { this.value = value; } public boolean isRegex() { return regex; } public void setRegex(boolean regex) { this.regex = regex; } }
项目:JFinal
假定输出是这样的:
columns[0][data]:
columns[0][name]:
columns[0][searchable]: true
columns[0][orderable]: true
那么代码是:
getBean(DataSrc.class, "columns[0]", true);
注意,如果你的 DataSrc 有相应的 setter 方法就用 getBean,否则用 getModel
回到你的具体需求,你的 columns[X] 中的 X 值是变化的,所以,你只需要用一个 for 循环即可:
for (int X=0; X《 len; X++) {
DataSrc ds = getBean(DataSrc.class, "columns[" + X + "]", true);
list.add(ds);
}
最后,你的 DataSrc 下面还有 List《Column》 columns, 这种多级 model 结构,这种场景,使用 json 传数据是最好的,具体传递方式见下一条回复