当前位置: 代码迷 >> Web前端 >> 对界面的打包 属性的封闭
  详细解决方案

对界面的打包 属性的封闭

热度:135   发布时间:2012-08-28 12:37:01.0
对界面的封装 属性的封闭

package com.sdjt.util.page;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

public class PageData extends HashMap implements Map{
	/**
	 * 自动将页面数据放入PageData对象中,当从页面获取数据时 new的时候要传request
	 * request.getParameter(arg0): get(arg0)或getString(arg0)
	 * request.getParameterValues(arg0): getValues(arg0)方法
	 * 本类也可以当做一个普通的Map使用,new的时候不要传参数
	 * author:lqf
	 */
	private static final long serialVersionUID = 1L;
	Map map = null;
	HttpServletRequest request;
	//从页面获取数据
	public PageData(HttpServletRequest request){
		this.request = request;
		map = request.getParameterMap();
		//当表单提交类型为"multipart/form-data"时,request.getParameterMap()返回的是一个HashMap,没有setLocked方法
		if("ParameterMap".equals(map.getClass().getSimpleName())) {
			Method method;
			try {
				method = map.getClass().getMethod("setLocked",new Class[]{boolean.class});
				method.invoke(map,new Object[]{new Boolean(false)});
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}
	}
	
	//普通的Map
	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;
	}
	
	//本方法只能用于从页面获取数组,例如相同名字的checkbox
	public String[] getValues(Object key) {
		return request == null ? null:request.getParameterValues((String)key);
	}
	
	public String getString(Object key) {
		return (String)get(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
		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();
	}
}

  相关解决方案