日期:2014-05-16  浏览次数:20370 次

Extjs 分页显示(SSH + json)
Extjs 分页显示(SSH + json)



1.自定义json处理类:处理查询出来的teams 和 传进来的 pageInfo对象
  (该类是模拟json的jar包)
package com.org.momo.util;

import java.util.List;

import com.org.momo.bean.PageInfo;
import com.org.momo.bean.Student;
import com.org.momo.bean.Team;

public class JsonUtil {
//teams开始
		strBuffer.append("\"teams\":[");
		//处理teams
		for(int i=0;i<teams.size();i++)
		{
			Team team = teams.get(i); 
			//team开始
			strBuffer.append("{");
			//处理id
			strBuffer.append("\"id\":");
			strBuffer.append(team.getId());
			strBuffer.append(",");
			//处理name
			strBuffer.append("\"name\":\"");
			strBuffer.append(team.getName());
			strBuffer.append("\",");
			//处理leader
			strBuffer.append("\"leader\":\"");
			strBuffer.append(team.getLeader());
			strBuffer.append("\",");
			//处理slogan
			strBuffer.append("\"slogan\":\"");
			strBuffer.append(team.getSlogan());	
			strBuffer.append("\",");
			//处理members
			strBuffer.append("\"members\":\"") ;
			if(team.getMembers()!=null){
				int j=0 ;
				for(Student stu:team.getMembers()){
					j++ ;
					strBuffer.append(stu.getName()) ;
					if(j<team.getMembers().size()){
						strBuffer.append(",") ;
					}
				}
			}
			strBuffer.append("\"") ;
			
			//team结束
			if(i<teams.size()-1)
			{
			strBuffer.append("},");
			}
			else
			{
				strBuffer.append("}");
			}
			
		}
		
		//teams结束
		strBuffer.append("]");
		
		
		strBuffer.append("}") ;
		jsonStr = strBuffer.toString() ;
		return jsonStr ;
	}
}



2.控制层处理类action:
package com.org.momo.action.team;

import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.org.momo.bean.PageInfo;
import com.org.momo.bean.Team;
import com.org.momo.service.TeamService;
import com.org.momo.util.JsonUtil;
import com.org.momo.util.XmlUtil;

public class ViewTeamsEXTAction extends ActionSupport {
	@Resource
	private TeamService teamService;
	private PageInfo pageInfo ;
	private Integer start ;
	private Integer limit ;
	
	public PageInfo getPageInfo() {
		return pageInfo;
	}

	public void setPageInfo(PageInfo pageInfo) {
		this.pageInfo = pageInfo;
	}

	public String execute() {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/plain;charset=GBK");
		try {
			PrintWriter out = response.getWriter();
			
			pageInfo = new PageInfo();
			pageInfo.setCurrentPage(start/5 + 1) ;   //设置当前页
			pageInfo.setPageRows(5) ;   //设置每页显示多少行
			//获取所有小组
			List<Team> teams = teamService.pageTeams(pageInfo) ;
			out.println(JsonUtil.teamsToExtPage(teams, pageInfo));  //将document转换为String输出
			out.flush() ;
			out.close() ;
		} catch (Exception e) {
			e.printStackTrace() ;
		}
		
		return NONE;
	}

	public void setStart(Integer start) {
		this.start = start;
	}

	public Integer getStart() {
		return start;
	}

	public void setLimit(Integer limit) {
		this.limit = limit;
	}

	public Integer getLimit() {
		return limit;
	}
}





3.struts.xml配置路径:
<action name="ViewTeamsEXT" class="com.org.momo.action.team.ViewTeamsEXTAction">
	</action>



4.显示层:
 <%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%>
<%
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
%>
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>显示小组(Ajax)</