PageData类

在这里插入代码片
package com.fh.util;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

public class PageData extends HashMap implements Map, Serializable  {

	public static Logger logger =  Logger.getLogger(PageData.class);
	
	private static final long serialVersionUID = 1L;

	Map map = null;
	HttpServletRequest request;

	/**
	 * 解析request中的请求参数
	 * @param request
	 */
	public PageData(HttpServletRequest request) {
		this.request = request;
		Map properties = request.getParameterMap();
		Map returnMap = new HashMap();
		Iterator entries = properties.entrySet().iterator();
		Map.Entry entry;
		String name = "";
		String value = "";
		while (entries.hasNext()) {
			entry = (Map.Entry) entries.next();
			name = (String) entry.getKey();
			Object valueObj = entry.getValue();
			if (null == valueObj) {
				value = "";
			} else if (valueObj instanceof String[]) {
				String[] values = (String[]) valueObj;
				for (int i = 0; i < values.length; i++) {
					value = values[i] + ",";
				}
				value = value.substring(0, value.length() - 1);
			} else {
				value = valueObj.toString();
			}
			if (name.equals("timeEnd")) {
				if (!value.equals("")) {
					value = value + " 23:59:59";
				} else {

				}

			}
			returnMap.put(name, value);
		}
		map = returnMap;
	}

	/**
	 * 解析json中的请求参数
	 * @param jo
	 */
	public PageData(JSONObject jo) {
		Map returnMap = new HashMap();
		for (Iterator<String> keys = jo.keys(); keys.hasNext();) {
			try {
				String key1 = keys.next();
//				System.out.println("key1---" + key1 + "------" + jo.get(key1) + (jo.get(key1) instanceof JSONObject)
//						+ jo.get(key1) + (jo.get(key1) instanceof JSONArray));
//				System.out.println("key1:" + key1 + "----------jo.get(key1):" + jo.get(key1));
				returnMap.put(key1.toUpperCase(), jo.get(key1));
			} catch (JSONException e) {
				logger.error("后台异常",e);
			}

		}
		map = returnMap;
	}
	
	public PageData(JSONObject jo, boolean flag) {
		Map returnMap = new HashMap();
		for (Iterator<String> keys = jo.keys(); keys.hasNext();) {
			try {
				String key1 = keys.next();
				if (flag) {
					returnMap.put(key1.toUpperCase(), jo.getString(key1));
				} else {
					returnMap.put(key1, jo.getString(key1));
				}
			} catch (JSONException e) {
				logger.error("后台异常",e);
			}

		}
		map = returnMap;
	}
	

	public PageData(org.json.JSONObject jo) {
		Map returnMap = new HashMap();
		for (Iterator<String> keys = jo.keys(); keys.hasNext();) {
			try {
				String key1 = keys.next();
//				System.out.println("key1---" + key1 + "------" + jo.get(key1) + (jo.get(key1) instanceof JSONObject)
//						+ jo.get(key1) + (jo.get(key1) instanceof JSONArray));
//				System.out.println("key1:" + key1 + "----------jo.get(key1):" + jo.get(key1));
				returnMap.put(key1.toUpperCase(), jo.get(key1));
			} catch (JSONException e) {
				logger.error("后台异常",e);
			}

		}
		map = returnMap;
	}
	
	
	public PageData() {
		map = new HashMap();
	}

	@Override
	public Object get(Object key) {
		Object obj = null;
		if (map.get(key) instanceof Object[]) {
			Object[] arr = (Object[]) map.get(key);
			obj = request == null ? arr : (request.getParameter((String) key) == null ? arr : arr[0]);
		} else {
			obj = map.get(key);
		}

		return obj;
	}

	public String getString(Object key) {
		// return String.valueOf(get(key)).toString();
		// System.out.println();
		// if(key instanceof String){
		// return (String) get(key);
		// } else if(key instanceof Integer){
		// return Integer.toString((int) key);
		// }
		// return (String) get(key);
		if (null != get(key) && "null" != get(key)) {
			return String.valueOf(get(key));
		}
		return "";
	}

	public double getDouble(Object key) {
		Object value = get(key);
		if (null != value && !"null".equals(value) && !"".equals(value)) {
			if (value instanceof String) {
				return Double.parseDouble(value.toString());
			} else if (value instanceof Double) {
				return (double) value;
			}
			// return (double) get(key);
		}
		return 0.0;
	}

	public int getInt(Object key) {
		Object value = get(key);
		if (null != value && !"null".equals(value) && !"".equals(value)) {
			if (value instanceof String) {
				return Integer.parseInt(value.toString());
			} else if (value instanceof Integer) {
				return (int) value;
			}
		}
		return 0;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Object put(Object key, Object value) {
		return map.put(key, value);
	}

	@Override
	public Object remove(Object key) {
		return map.remove(key);
	}

	public void clear() {
		map.clear();
	}

	public boolean containsKey(Object key) {
		// TODO Auto-generated method stub
		return map.containsKey(key);
	}

	public boolean containsValue(Object value) {
		// TODO Auto-generated method stub
		return map.containsValue(value);
	}

	public Set entrySet() {
		// TODO Auto-generated method stub
		return map.entrySet();
	}

	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return map.isEmpty();
	}

	public Set keySet() {
		// TODO Auto-generated method stub
		return map.keySet();
	}

	@SuppressWarnings("unchecked")
	public void putAll(Map t) {
		// TODO Auto-generated method stub
		map.putAll(t);
	}

	public int size() {
		// TODO Auto-generated method stub
		return map.size();
	}

	public Collection values() {
		// TODO Auto-generated method stub
		return map.values();
	}
	
	public Map getMap() {
		return this.map;
	}
	
	// 测试用
	public static void print(PageData pd) {
		System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" + pd.size()
				+ "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
		Iterator entries = pd.entrySet().iterator();
		Map.Entry entry;
		String name = "";
		String value = "";
		String printStr = "{";
		while (entries.hasNext()) {
			entry = (Map.Entry) entries.next();
			name = (String) entry.getKey();
			Object valueObj = entry.getValue();
			if (null == valueObj) {
				value = "";
			} else if (valueObj instanceof String[]) {
				String[] values = (String[]) valueObj;
				for (int i = 0; i < values.length; i++) {
					value = values[i] + ",";
				}
				value = value.substring(0, value.length() - 1);
			} else {
				value = valueObj.toString();
			}
			printStr += name + ":" + value + ",";
		}

		printStr += "}";

		System.out.println("PD::::::::" + printStr);
	}

}

package com.model; import javax.servlet.http.HttpServletRequest; import java.util.*; /** * 说明:参数封装Map * @version */ public class PageData extends HashMap implements Map{ private static final long serialVersionUID = 1L; Map map = null; HttpServletRequest request; public PageData(HttpServletRequest request){ this.request = request; Map properties = request.getParameterMap(); Map returnMap = new HashMap(); Iterator entries = properties.entrySet().iterator(); Entry entry; String name = ""; String value = ""; while (entries.hasNext()) { entry = (Entry) entries.next(); name = (String) entry.getKey(); Object valueObj = entry.getValue(); if(null == valueObj){ value = ""; }else if(valueObj instanceof String[]){ String[] values = (String[])valueObj; for(int i=0;i<values.length;i++){ value = values[i] + ","; } value = value.substring(0, value.length()-1); }else{ value = valueObj.toString(); } returnMap.put(name, value); } map = returnMap; } public PageData() { map = new HashMap(); } @Override public Object get(Object key) { Object obj = null; if(map.get(key) instanceof Object[]) { Object[] arr = (Object[])map.get(key); obj = request == null ? arr:(request.getParameter((String)key) == null ? arr:arr[0]); } else { obj = map.get(key); } return obj; } public static boolean hasVal(PageData pd,String key){ return pd!=null&&pd;.containsKey(key)&&!pd.getString(key).isEmpty(); } public String getString(Object key) { return (String)get(key); } public Integer getInt(Object key) { return Integer.parseInt(getString(key)); } @SuppressWarnings("unchecked") @Override public Object put(Object key, Object value) { return map.put(key, value); } @Override public Object remove(Object key) { return map.remove(key); } public void clear() { map.clear(); } public boolean containsKey(Object key) { // TODO Auto-generated method stub
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值