DEMO用的是ssh框架实现的,具体怎么搭建的就不多做说明了。分页表格的数据操作难点就是数据展现。至于增删改直接用hibernate原生的方法实现即可。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
/**
* @param pageSize
* 每页显示多少条
* @param currentPage
* 当前页
* @param paramMap
* 参数
* @param sidx
* 排序的列
* @param sord
* 升序or降序
* @return
*/
public
Map<?, ?> queryByJQGrid(
int
pageSize,
int
currentPage, Map<String, String> paramMap, String sidx, String sord);
|
接着我们来实现这个接口。
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
|
@Override
public
Map<?, ?> queryByJQGrid(
int
pageSize,
int
currentPage, Map<String, String> paramMap, String sidx, String sord) {
Map result =
new
HashMap();
List list =
null
;
try
{
Criteria cr = DetachedCriteria.forClass(Student.
class
).getExecutableCriteria(
this
.getSession());
if
((paramMap !=
null
) && (paramMap.size() >
0
)) {
String key =
null
;
String value =
null
;
Iterator ite = paramMap.keySet().iterator();
while
(ite.hasNext()) {
key = (String) ite.next();
value = (String) paramMap.get(key);
if
((key !=
null
) && (key.trim().length() >
0
) && (value !=
null
) && (value.trim().length() >
0
)) {
cr.add(Restrictions.like(key,
"%"
+ value.trim() +
"%"
));
}
}
}
Integer totcount = (Integer) cr.setProjection(Projections.rowCount()).uniqueResult();
cr.setProjection(
null
).setResultTransformer(Criteria.ROOT_ENTITY);
if
(!StringUtils.isEmpty(sidx)) {
list = cr.addOrder(((sord !=
null
) && (sord.trim().equalsIgnoreCase(
"asc"
))) ? Order.asc(sidx.split(
" "
)[
0
]) :
Order.desc(sidx.split(
" "
)[
0
])).setFirstResult((currentPage -
1
) * pageSize).setMaxResults(pageSize).list();
}
result.put(
"totalCount"
, totcount);
result.put(
"dataList"
, list);
}
catch
(Exception e) {
e.printStackTrace();
}
return
result;
}
|
1
2
3
4
5
6
7
8
|
protected
List dataList = Collections.emptyList();
//数据集合
protected
Integer rows = Integer.valueOf(
0
);
//行数
protected
Integer page = Integer.valueOf(
0
);
//当前页数
protected
Integer total = Integer.valueOf(
0
);
//数据总页数
protected
Integer record = Integer.valueOf(
0
);
//数据总条数
protected
String sord;
//排序方式
protected
String sidx;
//排序字段
protected
String search;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public
String query() {
Map<String, String> paramMap =
new
HashMap<String, String>();
Map map =
null
;
if
(
null
!= stuName && stuName.trim().length() >
0
) {
paramMap.put(
"name"
, stuName);
}
map = queryAllStudentService.queryByJQGrid(rows, page, paramMap, sidx, sord);
this
.dataList = (ArrayList) map.get(
"dataList"
);
Object totalCount = map.get(
"totalCount"
);
this
.record = Integer.valueOf(Integer.parseInt((totalCount ==
null
) ?
"0"
: totalCount.toString()));
this
.total = Integer.valueOf((
int
) Math.ceil(
this
.record.intValue() /
this
.rows.intValue()));
return
"query_success"
;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<
package
name
=
"student"
extends
=
"json-default"
>
<
action
name
=
"QueryActionUrl_*"
method
=
"{1}"
>
<
result
name
=
"query_success"
type
=
"json"
>
<
param
name
=
"includeProperties"
>
dataList\[\d+\]\.id,
dataList\[\d+\]\.name,
dataList\[\d+\]\.age,
dataList\[\d+\]\.address,
dataList\[\d+\]\.class,
dataList\[\d+\]\.likedo,
dataList\[\d+\]\.phone,
dataList\[\d+\]\.sex,
rows, page, total,record</
param
>
<
param
name
=
"noCache"
>true</
param
>
<
param
name
=
"ignoreHierarchy"
>false</
param
>
<
param
name
=
"contentType"
>text/html</
param
>
</
result
>
</
action
>
</
package
>
|
注意:在Struts2中使用json还需要json-plagin.jar的支持。不然会抛There is no mapping for namespace异常。如果还有什么不明白的地方,留下你的评论。
原创文章,转载请注明: 转载自java开发者
本文链接地址: Jqgrid入门-结合Struts2+json实现数据展示(五)