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)</title>

<link rel="stylesheet" type="text/css" href="css/team.css" />
<link rel="stylesheet" type="text/css" href="ext-2.0.2/resources/css/ext-all.css"/>

<script type="text/javascript" src="ext-2.0.2/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="ext-2.0.2/ext-all.js"></script>
<script type="text/javascript" src="js/trim.js"></script>

<script type="text/javascript">
Ext.onReady(function(){
    var store = new Ext.data.Store({   //Store开始
      proxy:new Ext.data.HttpProxy({url:"team/ViewTeamsEXT",method:"post"}),
      reader:new Ext.data.JsonReader(
                              {root:"teams",totalProperty:'totalProperty',id:"id"},
                              [{name:"id"},
                               {name:"name"},
                               {name:"leader"},
                               {name:"slogan"},
                               {name:"members"},
                               {name:"operation"}])
                                  }) ; //Store结束

   var column = new Ext.grid.ColumnModel([{header:'编号',dataIndex:'id'},
                                          {header:'组名',dataIndex:'name'},
                                          {header:'组长',dataIndex:'leader'},
                                          {header:'口号',dataIndex:'slogan'},
                                          {header:'组员',dataIndex:'members'},
                                          {header:'操作',dataIndex:'operation'}]) ;
   column.defaultSortable = true ;//默认可排序
   
   var grid = new Ext.grid.GridPanel({
        renderTo:"teamExt",
        width:800,
        autoHeight:true,
        title:'小组管理',
        store:store,
        cm:column,
        autoExpandColumn:4,
        bbar:new Ext.PagingToolbar({pageSize:5,store:store,displayInfo:true,displayMsg:'第{0}条到第{1}条,一共{2}条',emptyMsg:'没有记录'})}) ;
        
   store.load({params:{start:0,limit:5}});
}) ;
</script>
</head>
<body>
<div id="top">
<h1>显示小组</h1>
</div>
<div id="content">
<div id="teamExt"></div>
</div>
<p><div id="page"></div><br/>
<p><a href="team/index.jsp">返回小组管理</a>
</body>
</html>


你可能感兴趣的:(json extjs ssh)