自定义封装类PageData

PageData类,它继承了HashMap,外加实现了Map,用来自动将前台传递过来的数组参数自动装换成Map对象(PageData的对象),这样做的 好处是每次不用手动去解析前台传递的数据了,直接用this.PageData.getString("属性名")即可获取到值,很方便,,,请看代码

1.首先写个BaseControl类,里面需要有获取当前请求的HttpServletRequest对象方法,跟PageData类方法

/**
 * 
 */
package com.ssm.util;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;
import com.ssm.util.PageData;
/**
 *  @description
 *
 * @author Gaogzh
 *
 * @date 2018-4-14
 */
public class BaseController {
	protected Logger logger = Logger.getLogger(this.getClass());

	private static final long serialVersionUID = 6357869213649815390L;
	
	/**
	 * 得到PageData
	 */
	public PageData getPageData(){
		return new PageData(this.getRequest());
	}
	
	/**
	 * 得到ModelAndView
	 */
	public ModelAndView getModelAndView(){
		return new ModelAndView();
	}
	
	/**
	 * 得到request对象
	 */
	public HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		return request;
	}

	/**
	 * 得到32位的uuid
	 * @return
	 */
	/*public String get32UUID(){
		
		return UuidUtil.get32UUID();
	}
	
	*//**
	 * 得到分页列表的信息 
	 *//*
	public Page getPage(){
		
		return new Page();
	}*/
	
	public static void logBefore(Logger logger, String interfaceName){
		logger.info("");
		logger.info("start");
		logger.info(interfaceName);
	}
	
	public static void logAfter(Logger logger){
		logger.info("end");
		logger.info("");
	}

}

2.自定义类PageData的编写

/**
 * 
 */
package com.ssm.util;

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;

/**
 *  @description
 *
 * @author Gaogzh
 *
 * @date 2018-4-14
 */
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(); 
		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

3.PageData在项目中的使用

自定义封装类PageData_第1张图片

获取PageData:  PageData pd=this.getPageData();

新建PageData:PageData pd2=new PageData();

你可能感兴趣的:(Java,EE)