前台jsp代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'ext.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <link rel="stylesheet" type="text/css" href="<%=basePath%>ext/resources/css/ext-all.css"> <script type="text/javascript" src="<%=basePath%>ext/ext-base.js"></script> <script type="text/javascript" src="<%=basePath%>ext/ext-all.js"></script> <script type="text/javascript" src="<%=basePath%>ext/ext-lang-zh_CN.js"></script> <script type="text/javascript"> Ext.onReady(function(){ var store=new Ext.data.JsonStore({ url:"<%=basePath%>lpm/step2.action", autoLoad:{params:{start:0,limit:10}}, //传递到后台的参数 fields:["userid","name"], root:"data", //从后台取得的数据 totalProperty:"count" //从后台取得的数据总量 }); var column=new Ext.grid.ColumnModel( [ {header:"编号",dataIndex:"userid",sortable:true}, {header:"姓名",dataIndex:"name",sortable:true} ] ); var grid = new Ext.grid.GridPanel({ renderTo:"hello", title:"Hello world", height:300, width:470, cm:column, store:store, autoExpandColumn:0, loadMask:true, frame:true, autoScroll:true, bbar:new Ext.PagingToolbar({ pageSize:10, store:store, displayInfo: true, displayMsg: '显示 {0}-{1}条记录,共 {2} 条', emptyMsg: "没有记录" }) }); }) </script> </head> <body> <div id="hello"></div> </body> </html>
后台action代码:
package com.china.action; import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import javax.persistence.Query; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.apache.struts2.ServletActionContext; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.Result; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; @Namespace("/lpm") public class Lpmcon extends ActionSupport{ @Action(value="step2" ) public String getRes2(){ HttpServletRequest request=ServletActionContext.getRequest(); try{ EntityManagerFactory ef=Persistence.createEntityManagerFactory("mysql"); EntityManager em=ef.createEntityManager(); Query qu=em.createQuery("from persion order by userid"); int count=qu.getResultList().size(); // List list=qu.getResultList(); String start=request.getParameter("start"); String limit=request.getParameter("limit"); List list=qu.setFirstResult(Integer.parseInt(start)) .setMaxResults(Integer.parseInt(limit)) .getResultList(); Map map=new HashMap(); map.put("data", list); map.put("count",count); JSONObject jalist=JSONObject.fromObject(map); System.out.println(jalist.toString()); HttpServletResponse hsr=ServletActionContext.getResponse(); PrintWriter pw=hsr.getWriter(); pw.print(jalist.toString()); }catch(Exception e){ e.printStackTrace(); } return null; } }