溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

java封裝前端查詢條件通用版

發(fā)布時間:2020-10-03 19:57:42 來源:腳本之家 閱讀:168 作者:jackethans 欄目:編程語言

本文實例為大家分享了java封裝前端查詢條件的具體代碼,供大家參考,具體內(nèi)容如下

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;
  }
 
}

 調(diào)用之處:

@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()+"%");
 }

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI