Request請求一些常用的封裝

package cn.wtu.broadcast.util;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.util.StringUtils;

/**
 * @author newlinfeng
 */
public class RequestUtils {	

	/**
	 * 返回字符串,刪除了首尾空格,如果不存在則返回null
	 * 
	 * @param request
	 * @param key
	 * @return
	 */
	public static String getRequestKeyValue(HttpServletRequest request, String key) {
		String value = request.getParameter(key);
		if (StringUtils.isEmpty(value)) {
			return StringUtils.trimWhitespace(value);
		}
		return value;
	}

	/**
	 * 返回字符串,刪除了首尾空格,如果不存在則返回null
	 * 
	 * @param request
	 * @param key
	 * @return
	 */
	public static String getSessionKeyValue(HttpServletRequest request,	String key) {
		String value = null;
		HttpSession session = request.getSession();
		if (session != null) {
			Object obj = session.getAttribute(key);
			if (!StringUtils.isEmpty(obj)) {
				value = String.valueOf(obj);
			}
		}
		return value;
	}
	
	/**
	 * bootstrapTable參數亂碼問題解決
	 * @param key
	 * @return
	 */
	public static String formatBootstrapTableEncoding(String key) {
		String value = key;
		if (key != null) {
			try {
				value = new String(key.getBytes("ISO-8859-1"), "UTF-8");
			} catch (UnsupportedEncodingException e) {
				value = key;
			} 
		}
		return value;
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章