Jqgrid入门-结合Struts2+json实现数据展示(五)

DEMO用的是ssh框架实现的,具体怎么搭建的就不多做说明了。分页表格的数据操作难点就是数据展现。至于增删改直接用hibernate原生的方法实现即可。

        初步分析:表格要实现分页,那么一页显示多少条数(PageSize)和当前页码(CurrentPage)这两个条件必不可少。为了实现点击任何一列的表头进行排序,那么排序的列名(Sidx)和排序规则(Sort)必不可少。有了这四个参数实现基本的数据展现应该不成问题了。但是如果数据量比较多的时候,为了方便查询。还得需要一个Map参数,用来保存页面的请求参数。比如根据编号查询,根据名称查询等。
        废话不多说,先看Dao接口定义。
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;
}
        Service层不需要做什么处理,就不多做说明了。接下来看Action如何实现。为了配合Jqgrid的JsonReader选项的参数,我们可以把JsonReader里面的参数用JavaBean保存起来,再用Action去继承JavaBean,省略get和set方法。
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;
        编写Action继承刚才的JavaBean,调用Dao层的方法即可。
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" ;
}
        关键代码的实现已经差不多了,接下来就要注意返回的数据格式了。因为我们需要的是JSON格式的数据。第一种:可以直接通过JSONObject把集合序列化成json格式的数据返回,第二种:可以在Struts.xml里面继承json-default,然后通过配置返回数据。推荐使用第二种,因为第一种会把所有的字段都序列化过来,对性能会有一定影响。Struts.xml配置如下:
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异常。如果还有什么不明白的地方,留下你的评论。

posted @ 2013-07-03 21:04  爱生活,爱编程  阅读(202)  评论(0编辑  收藏  举报