基于Struts2+Extjs3.0 实现简单分页
jopen
12年前
S1,首先要在GridPanel中添加PagingToolbar,它跟GridPanel只是包含的关系。本质上它们都共用一个JsonStore,从而产生联系
bbar: new Ext.PagingToolbar({ pageSize: 2, store: check_list_store, displayInfo: true, beforePageText:"第 ", afterPageText:" 页/共 {0} 页", displayMsg: '显示从{0}条数据到{1}条数据,共{2}条数据', emptyMsg: "没有数据" })
S2,JsonStore一定要指定记录的总数目
var check_list_store = new Ext.data.JsonStore({ url: '<%=basePath%>check/check_list!list', root: "rows", totalProperty: 'totalCount', idProperty: 'studentSubID', remoteSort: true, fields:["studentSubID", "studentNumber", "projectName", "taskName", "studentName", "submitFile", "submitFileName", {name:"submitDate"}, "submitPass","submitScore","submitForUS"] });
S3,用Firebug查看,你会发现Extjs会自动向服务器POST两个参数,不错这两个参数就是表示显示当前第start条开始,当前页有limit条数据!所以我们只要在服务器端接收这两个数据,并且返回相应的JSON数据就可以了!
private Map session; public Map getSession() { return session; } public void setSession(Map session) { this.session = session; } //分页 private String limit; private String start; public String getLimit() { return limit; } public void setLimit(String limit) { this.limit = limit; } public String getStart() { return start; } public void setStart(String start) { this.start = start; } private Map responseJson; public Map getResponseJson() { return responseJson; } public void setResponseJson(Map responseJson) { this.responseJson = responseJson; } // SERVICE public CheckPagingService checkPagingService; public CheckPagingService getCheckPagingService() { return checkPagingService; } @Resource(name = "checkPagingService") public void setCheckPagingService(CheckPagingService checkPagingService) { this.checkPagingService = checkPagingService; } // 返回JSON 用到的MAP LIST Map<String, Object> map = new HashMap<String, Object>(); List<Map<String, Object>> list = new ArrayList<Map<String,Object>>(); public String list() throws Exception { //通过教师 学生提交列表 String number = (String) getSession().get("number"); int count =checkPagingService.getCountOfStudentSubByTeacher(number); List<studentsub>studentSubList=checkPagingService.getPagingStudentSubByTeacher(number , getStart()==null?0:Integer.valueOf(getStart()), getLimit()==null?2:Integer.valueOf(getLimit())); if (studentSubList != null) { for (StudentSub ss : studentSubList) { Map<string, object=""> m = new HashMap<string, object="">(); m.put("studentSubID", ss.getStudentSubID()); m.put("studentNumber", ss.getStudent().getNumber()); m.put("studentName", ss.getStudent().getStudentName()); m.put("projectName", ss.getTask().getProject().getProjectName()); m.put("taskName", ss.getTask().getTaskName()); m.put("submitFileName", ss.getFileName()); m.put("submitFile", ss.getFileSrc()); m.put("submitDate", ss.getSubDate()); // 是否审核 评分 ,被展示 m.put("submitPass", ss.getIsPass()); m.put("submitScore", String.valueOf(ss.getScore())); m.put("submitForUS", ss.getIsForUS()); list.add(m); } } map.put("totalCount",count); map.put("rows", list); this.setResponseJson(map); return "list"; }
Service层
public List<StudentSub> getPagingStudentSubByTeacher(final String number,final int index, final int pageSize)throws Exception{ return getStudentSubDAO().getPagingStudentSubByTeacher(number, index, pageSize); }
DAO层
public List<StudentSub> getPagingStudentSubByTeacher(final String number,final int index, final int pageSize){ List resultList=this.getHibernateTemplate().executeFind(new HibernateCallback() { public Object doInHibernate(Session s) throws HibernateException,SQLException { Query q=s.createQuery("from StudentSub ss where ss.task.project.teacher .number="+number+"order by ss.isPass asc"); q.setFirstResult(index); q.setMaxResults(pageSize); List list=q.list(); return list; } }); return resultList; }