jQuery EasyUI + struts2.3 + mongoDB 列表查询翻页JAVA样例

列表界面:

jQuery EasyUI + struts2.3 + mongoDB  列表查询翻页JAVA样例

主要实现方式:前台组合json格式查询条件,提交至后台解析处理

一.前台搜索脚本

String.prototype.replaceAll = function (s1, s2) {
    return this.replace(new RegExp(s1,"gm"),s2);
};

function doMongoSearch(){
 var whereCond = new Object();
 $("input[query],select[query]").each(function (index, domEle) {
  var type = $(domEle).attr('type');
  var query = $(domEle).attr('query').toUpperCase();
  var name = $(domEle).attr('name');
  var value = $(domEle).attr('value');
  var checked = $(domEle).attr('checked');
  var classVal = $(domEle).attr('class');
  if (classVal != undefined){
   if (classVal.indexOf("easyui-combobox") != -1){
    value = $(domEle).combobox('getValue');
    name = $(domEle).attr('id');
    type = "text";
   }else if (classVal.indexOf("easyui-datebox") != -1){
    value = $(domEle).datebox('getValue');
    name = $(domEle).attr('id');
    type = "text";
   }else if (classVal.indexOf("easyui-numberbox") != -1){
    value = $(domEle).numberbox('getValue');
    name = $(domEle).attr('id');
    type = "text";
   }
  }
  if((type =="text" || type=="select-one" || type=="hidden") && value != ""){
   if (query == "BEGIN_DATE"){
    whereCond["B$_"+name] = ">=@" + value.replaceAll("-","");
   }else if (query == "END_DATE"){
    whereCond["E$_"+name] = "<=@" + value.replaceAll("-","");
   }else if (query == "BEGIN_TIME"){
    whereCond["B$_"+name] = ">=@" + value.replaceAll("-","")+ "000000000";
   }else if (query == "END_TIME"){
    whereCond["E$_"+name] = "<=@" + value.replaceAll("-","")+ "235959999";
   }else{
    whereCond[name] = query + "@" + value;
   }
  } else if ((type=="checkbox" || type=="radio") && checked){
   whereCond[name] = query + "@" + value;
  }
 });
 var queryParams = $('#list_data').datagrid('options').queryParams;
 queryParams.whereCond = JSON.stringify(whereCond);
 $('#list_data').datagrid('getPager').pagination('select', 1);
}

/**
 * 查询条件清除
 * @return
 */
function cleanSearch(){
 $("input[query],select[query]").each(function (index, domEle) {
  var type = $(domEle).attr('type');
  var classVal = $(domEle).attr('class');
  if(type == undefined || type =="text" || type=="select-one"){
   if (classVal != undefined){
    if (classVal.indexOf("easyui-combobox") != -1){
     $(domEle).combobox('clear');
    }else if (classVal.indexOf("easyui-datebox") != -1){
     $(domEle).datebox('clear');
    }else if (classVal.indexOf("easyui-numberbox") != -1){
     $(domEle).numberbox('clear');
    }else{
        $(domEle).val("");
    }
   }else{
    $(domEle).val("");
   }
  }else if(type =="checkbox" || type=="radio"){
   $(domEle).attr("checked",false);
  }
 });
}

二.前台html关键代码

<div class="easyui-layout" data-options="fit:true">
            <div data-options="region:'center',border:false" style="padding:1px;">
               <table >
                   <tr>
                       <td>手机号码:</td>
                       <td><input type="text" name="mobile" query="LIKE" style="width: 100px;" ></td>
                   </tr>
                    <tr>
                       <td>运营商:</td>
                       <td>
                         <input id="operator" name="operator" query="=INT" class="easyui-combobox" data-options="
                             valueField:'dictValue',
                             textField:'opName',
                             width:100,
                             url:'/manage/dict.do?action=loadValueName&bean.opId=yys&bean.dictLevel=2'"/>
        </td>
                   </tr>
                    <tr>
                       <td>归属省:</td>
                       <td>
         <input id="province" name="province" query="="  class="easyui-combobox" data-options="
                             valueField:'opId',
                             textField:'opName',
                             width:100,
                             url:'/manage/dict.do?action=loadIdName&bean.opId=cn&bean.dictLevel=2',
                             onSelect: function(node){ 
                 var url = '/manage/dict.do?action=loadIdName&bean.opId='+node.opId;
                 $('#city').combobox('clear');
                 $('#city').combobox('reload', url); 
             }"/>
     </td>
                   </tr> 
                   <tr>
                       <td>归属市:</td>
                       <td>
                         <input id="city" name="city" query="=" class="easyui-combobox"
                         data-options="valueField:'opId',textField:'opName',width:100" />
     </td>
                   </tr>
                   <tr>
                       <td>号码来源:</td>
                       <td>
                         <input id="source" name="source" query="=INT"  class="easyui-combobox" data-options="
                             valueField:'opId',
                             textField:'siteName',
                             width:100,
                             url:'/manage/mobilesite.do?action=loadSourceName'
                             "/>
     </td>
                   </tr>
                   <tr>
                       <td >发布日期从</td>
                       <td><input type="text" name="publish" id="publish" class="easyui-datebox" query="BEGIN_DATE" style="width: 100px;" ></td>
                   </tr>
                   <tr>
                       <td align="right">至</td>
                       <td><input type="text" name="publish" id="publish" class="easyui-datebox" query="END_DATE" style="width: 100px;" ></td>
                   </tr>
              </table>
            </div>
            <div data-options="region:'south',border:false" style="text-align: center; height: 30px; line-height:10px;">
               <a class="easyui-linkbutton" data-options="iconCls:'icon-search'" href="javascript:void(doMongoSearch())">搜索</a>
               <a class="easyui-linkbutton" data-options="iconCls:'icon-no'"  href="javascript:void(cleanSearch())">清空</a>
            </div>
          </div>

三.后台action方法

public String doPage() {
        pageData = new BN_Page("_id", "t_mobile_all", rows, page, sort, order, whereCond);
       pageData = DAO_MobileQuery.getPage(pageData);
       Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map  
       jsonMap.put("total",pageData.getRowCount());
       jsonMap.put("rows", pageData.getData());
       jsonResult =JSONObject.fromObject(jsonMap);
      return "doJSON";
 }

后台DAO_MobileQuery方法

public static BN_Page getPage(BN_Page page) {
  return new DAOMongo().doSelectPage(page, BN_Mobile.class);
 }

后台BN_Page.java关键代码

private ArrayList data = new ArrayList();
    private int rows = 15;
    private int page = 1;

private String whereCond = "";
    private int rowCount;

private String tableName = null;
   private String sort = "";
   private String order = "";

public BN_Page(String keyColumn, String tableName, String rows, String page,
      String sort, String order, String whereCond)
    {
        this.keyColumn = keyColumn;
        this.tableName = tableName;
        this.rows = Integer.parseInt(rows);
        this.page = Integer.parseInt(page);
        this.sort = sort;
        this.order = order;
        this.whereCond = whereCond;
    }

.....常规get/set 方法

后台DAOMongo.java关键方法

public BN_Page doSelectPage(BN_Page page, Class beanClass){
  DBCollection connection = db.getCollection(page.getTableName());
  DBObject queryObject = new BasicDBObject();
  if (!ObjectUtil.isNull(page.getWhereCond())){
   logger.info("whereCond="+page.getWhereCond());
   List<Map<String, Object>> whereCondList = JsonUtil.json2List("["+page.getWhereCond()+"]");
   Map<String, Object> map = whereCondList.get(0);
   Set<String> set = map.keySet();
   for (Iterator<String> it = set.iterator();it.hasNext();) {
    String queryColumn = it.next();
    String queryValue = map.get(queryColumn).toString();
    if (queryColumn.startsWith("B$_") || queryColumn.startsWith("E$_")){
     queryColumn = queryColumn.substring(3);
    }
    if (queryValue.startsWith("LIKE@")){
     queryObject.put(queryColumn,
       Pattern.compile("^.*"+queryValue.substring(5)+".*$", Pattern.CASE_INSENSITIVE));
    }else if (queryValue.startsWith("=@")){
     queryObject.put(queryColumn, queryValue.substring(2));
    }else if (queryValue.startsWith("=INT@")){
     queryObject.put(queryColumn, new Integer(queryValue.substring(5)).intValue());
    }else if (queryValue.startsWith(">@")){
        if (queryObject.containsField(queryColumn)){
         ((BasicDBObject)queryObject.get(queryColumn)).append("$gt",queryValue.substring(2));
        }else{
        queryObject.put(queryColumn, new BasicDBObject("$gt",queryValue.substring(2)));
     }
    }else if (queryValue.startsWith(">INT@")){
     if (queryObject.containsField(queryColumn)){
         ((BasicDBObject)queryObject.get(queryColumn)).append("$gt",new Integer(queryValue.substring(5)).intValue());
        }else{
        queryObject.put(queryColumn, new BasicDBObject("$gt",new Integer(queryValue.substring(5)).intValue()));
     }
    }else if (queryValue.startsWith(">=@")){
     if (queryObject.containsField(queryColumn)){
         ((BasicDBObject)queryObject.get(queryColumn)).append("$gte",queryValue.substring(3));
        }else{
        queryObject.put(queryColumn, new BasicDBObject("$gte",queryValue.substring(3)));
     }
    }else if (queryValue.startsWith(">=INT@")){
     if (queryObject.containsField(queryColumn)){
         ((BasicDBObject)queryObject.get(queryColumn)).append("$gte",new Integer(queryValue.substring(6)).intValue());
        }else{
        queryObject.put(queryColumn, new BasicDBObject("$gte",new Integer(queryValue.substring(6)).intValue()));
     }
    }else if (queryValue.startsWith("<@")){
     if (queryObject.containsField(queryColumn)){
         ((BasicDBObject)queryObject.get(queryColumn)).append("$lt",queryValue.substring(2));
        }else{
        queryObject.put(queryColumn, new BasicDBObject("$lt",queryValue.substring(2)));
     }
    }else if (queryValue.startsWith("<INT@")){
     if (queryObject.containsField(queryColumn)){
         ((BasicDBObject)queryObject.get(queryColumn)).append("$lt",new Integer(queryValue.substring(5)).intValue());
        }else{
        queryObject.put(queryColumn, new BasicDBObject("$lt",new Integer(queryValue.substring(5)).intValue()));
     }
    }else if (queryValue.startsWith("<=@")){
     if (queryObject.containsField(queryColumn)){
         ((BasicDBObject)queryObject.get(queryColumn)).append("$lte",queryValue.substring(3));
        }else{
        queryObject.put(queryColumn, new BasicDBObject("$lte",queryValue.substring(3)));
     }
    }else if (queryValue.startsWith("<=INT@")){
     if (queryObject.containsField(queryColumn)){
         ((BasicDBObject)queryObject.get(queryColumn)).append("$lte",new Integer(queryValue.substring(6)).intValue());
        }else{
        queryObject.put(queryColumn, new BasicDBObject("$lte",new Integer(queryValue.substring(6)).intValue()));
     }
    }else if (queryValue.startsWith("!=@")){
     queryObject.put(queryColumn, new BasicDBObject("$ne",queryValue.substring(3)));
    }else if (queryValue.startsWith("!=INT@")){
     queryObject.put(queryColumn, new BasicDBObject("$ne",new Integer(queryValue.substring(6)).intValue()));
    }
   }
  }

DBObject sortObject = new BasicDBObject();

sortObject.put(page.getSort(), page.getOrder().equals("asc")? 1 : -1);

logger.info("collection="+page.getTableName());

logger.info("queryObject="+queryObject.toString());

logger.info("sortObject="+sortObject.toString());

logger.info("limit="+page.getRows());

logger.info("skip="+page.getRowNo());

DBCursor cursor = connection.find(queryObject);

page.setRowCount(cursor.count());

cursor = cursor.sort(sortObject).limit(page.getRows()).skip(page.getRowNo());

try {

while (cursor.hasNext()){

DBObject object = cursor.next();

BN_Row row = (BN_Row) beanClass.newInstance();

row.columnMap.putAll(object.toMap());

page.addRow(row);

}

} catch (InstantiationException e)

{    // TODO Auto-generated catch block    e.printStackTrace();   } catch (IllegalAccessException e) {    // TODO Auto-generated catch block    e.printStackTrace();   } finally {    cursor.close();   }

db = null;

connection = null;

return page;

}

上一篇:使用 vi 命令


下一篇:我的AutoHotkey脚本