JSP页面
<script type="text/javascript"src="lib/jquery-1.8.0.min.js"></script>
<script type="text/javascript"src="lib/jquery.easyui.min.js"></script>
<script type="text/javascript"src="lib/locale/easyui-lang-zh_CN.js"></script>
<link rel="stylesheet"href="lib/themes/icon.css" type="text/css"></link>
<link rel="stylesheet" href="lib/themes/gray/easyui.css"type="text/css"></link>
<body>
<table id="testGrid"></table>
<div id="tool">
<a id="btn" href="javascript:add()" class="easyui-linkbutton"data-options="iconCls:'icon-add'">增加</a>
<a id="btn"href="javascript:del()" class="easyui-linkbutton"data-options="iconCls:'icon-remove'">删除</a>
<a id="btn"href="javascript:update()" class="easyui-linkbutton"data-options="iconCls:'icon-edit'">修改</a>
<a id="btn"href="javascript:search()" class="easyui-linkbutton"data-options="iconCls:'icon-search'">查找</a>
</div>
</body>
<script type="text/javascript">
//初始函数,用来显示所有的列表
$(function(){
$("#testGrid").datagrid({
url:'fenye1.action?time'+new Date().getTime(),
fitColumns:true,
toolbar:'#tool',
sortName:'id',
sortOrder:'desc',
pagination:true,//显示分页栏
rownumbers:true,//显示行号
columns:[[
{checkbox:true},
{field:'id',title:'id',width:100,sortable:true},
{field:'username',title:'username',width:100},
{field:'pwd',title:'pwd',width:100},
{field:'sex',title:'sex',width:100,
formatter:function(value,rowdata,index){
if(value=='f')
return"<font color='green'>女</font>";
else
return"<font color='red'>男</font>";
}
},
{field:'strbirthday',title:'birthday',width:100}
]]
});
//设置分页控件
varp = $('#testGrid').datagrid('getPager');
$(p).pagination({
pageSize: 5,//每页显示的记录条数,默认为5
pageList: [5,10,15],//可以设置每页记录条数的列表
beforePageText: '第',//页数文本框前显示的汉字
afterPageText: '页 共 {pages} 页',
displayMsg: '当前显示 {from} - {to} 条记录 共 {total} 条记录'})
})
</script>
思路数量
1. 首先,sql语句:
在TestDao接口中,定义分页查询方法:
public List<Test>findByNameAndPass(Test t);
在TestMapper.xml中定义该方法使用的三千里语句
<select id="findByPageApp" parameterType="com.test.action.Page" resultType="com.test.action.Test">
select * from (select rownum r,id,username,pwd,sex,birthdaystrbirthday from test) where r between #{start} and #{end}
</select>
由于在sql语句中用到了两个值start和end,将这两个值封装到一个类Page中:
Page类的位置:
Page类的内容:
2.使用EUI分页时,点击换页时,会自动上传两个值:
rows;//每页显示的记录数,默认值为1
page;//当前第几页,默认值为10
因此在action中需要设置属性名字一样的变量,接收这2个值
Action的编写
public class Fenye extends ActionSupport {
private Test t;
private String rows;//每页显示的记录数
private String page;//当前第几页
TestImpl buss =new TestImpl();
public Test getT() {
return t;
}
public void setT(Test t) {
this.t = t;
}
public String getRows() {
return rows;
}
public void setRows(String rows) {
this.rows = rows;
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
public String fenye(){
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//当前页
int intPage = Integer.parseInt(page);
//每页显示条数
int introws = Integer.parseInt(rows);
//每页的开始记录 第一页为1 第二页为number +1
int start =(intPage-1)*introws+1;
int end=intPage*introws;
Page p=new Page();
p.setStart(start);
p.setEnd(end);
int total=100;//记录总记录数,默认设置为100
total=buss.calCount();//计算出总记录数,并赋值给total
List<Test>list = buss. findByPageApp(p);//每页的数据,放入list
Map<String,Object> jsonMap = new HashMap<String, Object>();//定义map
jsonMap.put("total", total);//total键存放总记录数,必须的EUI会自动读取
jsonMap.put("rows", list);//rows键存放每页记录 list
String s=JSON.toJSONString(jsonMap);
try {
response.getWriter().print(s);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}