码迷,mamicode.com
首页 > 其他好文 > 详细

SSM框架使用Map来替代实体Entity

时间:2018-10-27 22:57:16      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:str   ati   war   request对象   string   数据   stc   stat   todo   

基础控制器

public class BaseController {

    /**
     * 根据当前Request得到PageData
     *
     * @return the page data
     */
    public PageData getPageData() {
        return new PageData(this.getRequest());
    }

    /**
     * 得到ModelAndView
     *
     * @return the model and view
     */
    public ModelAndView getModelAndView() {
        return new ModelAndView();
    }

    /**
     * 得到request对象
     * @return the request
     */
    public HttpServletRequest getRequest() {
        // 利用spring api获取当前request对象
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request;
    }
}

封装来用户代替实体的map对象

/**
 * 页面数据对象(相当于实体),注意:PageData实质就是HashMap
 * 获取页面数据封装到Map中作为实体对象
 */
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(); // String, String
        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();
            }
            
            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 String getString(Object key) {
        return (String) get(key);
    }
    public double getDouble(Object key) {
        return (double) get(key);
    }
    public String getNoneNULLString(Object key) {
        String s = (String) get(key);
        return s==null?"":s;
    }

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

    @Override
    public String toString() {
        return "PageData [map=" + map + ", request=" + request + "]";
    }

}

SSM框架使用Map来替代实体Entity

标签:str   ati   war   request对象   string   数据   stc   stat   todo   

原文地址:https://www.cnblogs.com/tandi19960505/p/9863768.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!