Sigma-grid分页的后台方法

 

1.客户端sigma-grid的loadURL:../../service?action=BaseQuery因为是示例,所以在SQL语句直接写在sigma-grid的gridOption中:

beforeLoad : function(reqParam , grid )
{
	reqParam.parameters={"LoadSQL":"select id,name,refid1 from test,"UpdateSQL":""}
},			
loadURL:'../../service?action=BaseQuery'

 服务端BaseQuery代码:

 

public class BaseQuery implements EAServletInterface {
          public String Response(HttpServletRequest request,HttpServletResponse response) throws Exception {
	ParseJSON parseJson ;
	String resultStr = "";
   	try{
		parseJson = new ParseJSON();
		Map gtJsonIn = parseJson.parseJSON2Map( request.getParameter("_gt_json") );   	
		resultStr = parseJson.getSplitPageJsonString(gtJsonIn);
	}catch(Exception e){
		e.printStackTrace();
	}
		
		return resultStr;
	}

}

 其中ParseJSON类的只要就是把JSON字符串或对象,转换成别的形式:Map,Array,POJO,XML.....

    public Map parseJSON2Map(JSONObject jsonObj){
        Iterator it = jsonObj.keys();
        String key;
        Object value;
        Map valueMap = new HashMap();
        while(it.hasNext()){
            key = (String) it.next();
            value = jsonObj.get(key);
            valueMap.put(key, value);

        }
        return valueMap;
    }    

 下面是转换成分面后的JSON字符串

 

public String getSplitPageJsonString(Map map) throws EAException{
	   	 EADatabase db = null;
		 EAXmlDS ds = null;
		 String ret = "";
    	try{
	    	 db = new EADatabase();
	    	 //从参数中查询出SQL
	    	 Map sqlMap = this.parseJSON2Map((JSONObject)map.get("parameters"));
	    	 String update_sql = (String) sqlMap.get("UpdateSQL");
	    	 if (update_sql.indexOf("update")>-1 || update_sql.indexOf("UPDATE")>-1){
	    		 db.ExcecutSQL(update_sql);
	    	 }
	    	 String sql = (String) sqlMap.get("LoadSQL");
	    	 Map r_pageInfo =  this.parseJSON2Map((JSONObject)map.get("pageInfo"));   
	    	 int totalRowNum= ((Integer) r_pageInfo.get("totalRowNum")).intValue();   
	    	 int startRowNum= ((Integer) r_pageInfo.get("startRowNum")).intValue();   
	    	 int pageSize= ((Integer) r_pageInfo.get("pageSize")).intValue();
	    	 int pageNum = ((Integer) r_pageInfo.get("pageNum")).intValue();

	    	 ds = db.QuerySQL(sql);	    	 
	    	 totalRowNum = ds.getRowCount();	    	 
	    	 int endRowNum = pageSize * pageNum;
	    	 if (endRowNum == totalRowNum){
	    		 endRowNum = totalRowNum;
	    	 }
	    	 //总页数
	    	 int totalPageNum = (totalRowNum+pageSize)/pageSize;
	    	 	    	
	    	 ArrayList data = new ArrayList();
	    	 String sqlstr = "select * from (select rownum rno,t.* from ("+sql+")t) where rno>="+startRowNum +" and rno<"+endRowNum;
		//这里的ds是写的一个二维的结果,类似于数据库表的数据结构
	    	 ds = db.QuerySQL(sqlstr);
	    	 for (int row = 0;row < ds.getRowCount() ;row++){	    		 
	    		 HashMap record = new HashMap();
	    		 for (int col = 0;col < ds.getColumnCount(); col++){
	    			 record.put(ds.getColumnName(col), ds.getStringAt(row,col));
	    		 }
    			 data.add(record);
	    	 }   	 
	    	 Map pageInfo = new HashMap();
	    	 pageInfo.put("pageSize", pageSize);
	    	 pageInfo.put("pageNum", pageNum);
	    	 pageInfo.put("totalRowNum", totalRowNum);
	    	 pageInfo.put("startRowNum", startRowNum);
	    	 pageInfo.put("endRowNum", endRowNum);
	    	 
	    	 Map gtJsonOut=new HashMap();   
    	     gtJsonOut.put("pageInfo",pageInfo);   
    	     gtJsonOut.put("data",data ); 
    	     
    	     ret =  this.getJsonString4Map(gtJsonOut);   

	    	 db.Commit();
    	}catch(Exception e){
    		db.Close();
    		e.printStackTrace();
    	}finally{
    		if ( db != null ){
    			db.Close();
    		}
    		
    	}

    	return ret;
    }

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(数据结构,sql,xml,json)