欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java封装前端查询条件通用版

程序员文章站 2024-02-23 09:34:16
本文实例为大家分享了java封装前端查询条件的具体代码,供大家参考,具体内容如下 import hengyi.oa.mobile.exception.serv...

本文实例为大家分享了java封装前端查询条件的具体代码,供大家参考,具体内容如下

import hengyi.oa.mobile.exception.serviceexception;
import java.io.unsupportedencodingexception;
import java.util.list;
import java.util.map;
import java.util.map.entry;
import javax.servlet.http.httpservletrequest;
import org.apache.commons.lang.stringutils;
 
 
/**
 * 查询条件封装类
 * @author  xue.yq
 * @created 2018年11月11日
 */
public class tabletagbean
{
 public static final int default_page_size = 20;
 
 private static final string delimiter = "\n";
 
 private int m_page;
 
 private int m_pagesize = default_page_size;
 
 private int m_total;
 
 private string m_orderby;
 
 private boolean m_asc;
 
 private stringmap m_condition = new stringmap();
 
 private list m_list;
 
 private boolean m_builded = false;
 
 private string m_conditionparam = null;
 
 private int m_pagecount;
 
 private boolean m_canprev;
 
 private boolean m_cannext;
 
 public tabletagbean() {}
 
 public boolean getasc() {
  return m_asc;
 }
 
 public void setasc(boolean asc) {
  ensurenotbuilded();
  m_asc = asc;
 }
 
 public stringmap getcondition() {
  return m_condition;
 }
 
 public void setcondition(stringmap condition) {
  ensurenotbuilded();
  m_condition = condition;
 }
 
 public string getorderby() {
  return m_orderby;
 }
 
 public void setorderby(string orderby) {
  ensurenotbuilded();
  m_orderby = orderby;
 }
 
 public int getpage() {
  return m_page;
 }
 
 public void setpage(int page) {
  ensurenotbuilded();
  m_page = page;
 }
 
 public int getpagesize() {
  return m_pagesize;
 }
 
 public void setpagesize(int pagesize) {
  ensurenotbuilded();
  m_pagesize = pagesize;
 }
 
 public int gettotal() {
  return m_total;
 }
 
 public void settotal(int total) {
  ensurenotbuilded();
  m_total = total;
 }
 
 public list getlist() {
  return m_list;
 }
 
 public void setlist(list list) {
  ensurenotbuilded();
  m_list = list;
 }
 
 public void ensurenotbuilded() {
  if (m_builded) {
   throw new illegalstateexception(
     "can't set attribute to tabletagbean after build() is called.");
  }
 }
 
 public boolean getcannext() {
  return m_cannext;
 }
 
 public boolean getcanprev() {
  return m_canprev;
 }
 
 public string getconditionparam() {
  return m_conditionparam;
 }
 
 public int getpagecount() {
  return m_pagecount;
 }
 
 public void build() throws unsupportedencodingexception {
 
  if (m_condition.size() > 0) {
   m_conditionparam = cryptoutil.base64url_encode(m_condition
     .tostring(delimiter).getbytes("gbk"));
  }
 
  if (m_total % m_pagesize != 0) {
   m_pagecount = m_total / m_pagesize + 1;
  } else {
   m_pagecount = m_total / m_pagesize;
  }
 
  m_canprev = m_page > 0;
  m_cannext = m_page < m_pagecount - 1;
 
  m_builded = true;
 }
 
 public static tabletagbean getfromrequest(httpservletrequest req)
   throws unsupportedencodingexception {
  tabletagbean r = new tabletagbean();
 
  string p = req.getparameter("p");
  if (stringutils.isnotempty(p) ) {
   r.setpage(integer.parseint(p));
  }
  string o = req.getparameter("o");
  if (stringutils.isnotempty(o)) {
   r.setorderby(o);
  }
  string a = req.getparameter("a");
  if (stringutils.isnotempty(a)) {
   r.setasc(a.equals("1"));
  }
  string c = req.getparameter("c");
  if (stringutils.isnotempty(c)) {
   r.setcondition(decodeconditionparam(c));
  }
 
  return r;
 }
 
 private static stringmap decodeconditionparam(string c)
   throws unsupportedencodingexception {
  string s = new string(cryptoutil.base64url_decode(c), "gbk");
  return new stringmap(s, delimiter);
 }
 
 public string getqueryparameter() {
  stringbuffer sb=new stringbuffer();
  
  sb.append("p="+m_page);
  if(m_orderby!=null) {
   sb.append("&o="+m_orderby+"&a="+(m_asc?"1":"0"));
  }
  
  if(m_conditionparam!=null) {
   sb.append("&c="+m_conditionparam);
  }
  
  return sb.tostring();
 }
 
 @override
 public string tostring() {
  stringbuffer sb = new stringbuffer();
 
  sb.append("[tabletagbean]\r\n");
  sb.append("m_page: " + m_page + "\r\n");
  sb.append("m_pagesize :" + m_pagesize + "\r\n");
  sb.append("m_total :" + m_total + "\r\n");
  sb.append("m_orderby :" + m_orderby + "\r\n");
  sb.append("m_asc :" + m_asc + "\r\n");
  sb.append("m_condition :" + m_condition + "\r\n");
  sb.append("m_list(size) :" + m_list.size() + "\r\n");
  sb.append("m_builded :" + m_builded + "\r\n");
  sb.append("m_conditionparam :" + m_conditionparam + "\r\n");
  sb.append("m_pagecount :" + m_pagecount + "\r\n");
  sb.append("m_canprev :" + m_canprev + "\r\n");
  sb.append("m_cannext :" + m_cannext + "\r\n");
 
  return sb.tostring();
 }
 
 @suppresswarnings({ "rawtypes", "unchecked" })
 public static tabletagbean getfromparameter(httpservletrequest request)
  throws serviceexception
  {
  string start = request.getparameter("start");
  string limit = request.getparameter("limit");
  string asc = request.getparameter("dir");
  string orderby = request.getparameter("sort");
  int page = (start != null) ? integer.parseint(start) : 0;
  int pagesize = (limit != null) ? integer.parseint(limit) : 30;
  boolean isasc = "asc".equals(asc);
 
  tabletagbean ttb = new tabletagbean();
  ttb.setpage(page);
  ttb.setpagesize(pagesize);
  ttb.setorderby(orderby);
  ttb.setasc(isasc);
   
  
  map<string, string[]> m = request.getparametermap();
  string isencodeuri = request.getparameter("isencodeuri");
  if (m != null) {
   for (entry entry : m.entryset()) {
   string key = (string)entry.getkey();
   object[] value = (object[])entry.getvalue();
   
//   if("y".equals(isencodeuri)){
//    string v= java.net.urldecoder.decode(value[0].tostring(), "utf-8");
//    ttb.getcondition().put(key, (value[0] == null) ? null :v);
//   }else{
    ttb.getcondition().put(key, (value[0] == null) ? null : value[0].tostring());
//   }
   }
  }
  return ttb;
  }
 
}

 调用之处:

@requestmapping("/listagentawaitrepor")
 public list<departmentofficedto> listagentawaitrepor(httpservletrequest request) throws serviceexception
 {
  tabletagbean ttb = tabletagbean.getfromparameter(request);
  return oldagentawaitreportsearchfacade.list(ttb);
 }

取值之处:

string titlename = ttb.getcondition().get("titlename");
 if(!genericvalidator.isblankornull(titlename))
 {
  sql.append(" and biz.biz_title like :biztitle ");
  sqlparams.put("biztitle", "%"+titlename.trim()+"%");
 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。