Struts2获取easyUI datagrid 分页参数page和rows
用pageHelper分页时,只要是能够获取前台传来的两个参数page和rows基本就完成了很大一部分。
获取方法:定义两个变量page和rows,设置对应的getter,setter方法,在方法中直接获取就能打得。easyUI默认的每次请求时都会传递这两个参数。
- public int getPage() {
- return page;
- }
- public void setPage(int page) {
- this.page = page;
- }
- public int getRows() {
- return rows;
- }
- public void setRows(int rows) {
- this.rows = rows;
- }
- public String getAllPages() {
- List<Emp> plist = empDao.findAll();
- PageHelper.startPage(page, rows);
- List<Emp> elist = empDao.findAll();
- // 判断
- if (elist.size() > 0) {
- map.put("total", plist.size());
- map.put("rows", elist);
- return SUCCESS;
- }
- return ERROR;
- }
返回的数据必须转成json格式
- <!-- 配置查询所有方法Action -->
- <action name="listAction" class="EmpAction" method="getAllPages">
- <result name="success" type="json">
- <param name="root">map</param>
- </result>
- </action>
springmvc
page,rows分页请求参数,rows和total结果参数
@RequestMapping(value = "/getSalaryList.rst", method = {RequestMethod.GET, RequestMethod.POST})
@ResponseBody
public Object getSalaryList (int page,int rows,@RequestParam(value="key",required=false) String key){
ICoreRequestContext ctx = CoreRequestContextUtil.getCoreRequestContext();
SSOUser ssoUser = ctx.getCurrentUser();
if (ssoUser.isAnonymous()) {
//return new Result(ResultCode.ERROR.getCode(),"用户未登录");
return alertError("用户未登录!");
}
HiberSession dbSession = null;
/* LostPropPermissionChecker.checkPermission(this.getAccessContext());*/
try {
dbSession = (HiberSession) CoreplusEnv.getDBSessionFactory().getDBSession();
dbSession.beginTransaction();
Map<String, Object> params = new HashMap<String, Object>();//参数集合
if(!StringUtil.isEmpty(key)){
params.put("key", "%"+key+"%");
}
params.put("begin", page-1);
params.put("length", rows);
LightAppDBUtil _dbUtil = LightAppDBUtil.getInstance();
Query query = _dbUtil.parserNamedQuery(dbSession, "getSalaryList", params);
//转译成需要的类Salary
ResultTransformer rt = Transformers.aliasToBean(Salary.class);
List<Salary> salaryList = query.setResultTransformer(rt).list();
JSONObject result= new JSONObject();
if (salaryList == null || salaryList.isEmpty()) {
result.put("total", 0);
result.put("rows", salaryList);
return result;
}
int total=inquiryTotal();
// jo.put("errMsg", "");
result.put("rows", salaryList);
result.put("total", total);
// return new DataGridVO(total,salaryList);
return result;
} catch (Exception e) {
_log.error("查询工资列表失败!", e);
dbSession.rollback();
return alertError("查询工资列表异常");
} finally {
closeSession(dbSession);
}
}