?? paramshelper.java
字號:
package com.onetsoft.fastjsp;
import com.onetsoft.fastjsp.request.PageData;
import com.onetsoft.fastjsp.util.Constants;
import com.onetsoft.fastjsp.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Iterator;
import java.util.Map;
public class ParamsHelper {
public static String getParameter(Map parametersMap, String name) {
Object o = parametersMap.get(name);
if (o == null) return null;
if (o instanceof Object[]) {
Object[] a = (Object[]) o;
if (a.length == 0) return StringUtils.EMPTY;
return a[0] == null ? null : a[0].toString();
} else {
return o.toString();
}
}
public static String[] getParameterValues(Map parametersMap, String name) {
Object o = parametersMap.get(name);
if (o instanceof String[]) {
return (String[]) o;
}
if (o instanceof Object[]) {
Object[] a = (Object[]) o;
String[] row = new String[a.length];
Object v;
for (int i = 0; i < row.length; i++) {
v = a[i];
row[i] = v == null ? null : v.toString();
}
return row;
} else
return o != null ? new String[]{o.toString()} : null;
}
/**
* 解析非上傳http請求所包含的參數
* 方便{@link PageData}直接取得參數
*
* @param request
* @param parametersMap
*/
static void decodeRequestParameterMap(HttpServletRequest request, Map parametersMap, String enc) {
boolean methodPost = StringUtils.METHOD_POST.equalsIgnoreCase(request.getMethod());
Map map = request.getParameterMap();
if (map.isEmpty()) return;
Map.Entry e;
String[] a;
for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
e = (Map.Entry) iter.next();
a = (String[]) e.getValue();
if (a.length == 0) {
parametersMap.put(e.getKey(), StringUtils.EMPTY);
} else if (a.length == 1) {
try {
// Debug.debug("before/after"+a[0]+"/"+ new String(a[0].getBytes(Constants.ISO_8859_1), enc));
/* 經測試 URL 參數似乎都是 8859_1 方式傳遞。*/
// parametersMap.put(e.getKey(), new String(a[0].getBytes(Constants.ISO_8859_1), enc)); //url
if (methodPost)
parametersMap.put(e.getKey(), URLDecoder.decode(a[0], enc)); // post request
else
parametersMap.put(e.getKey(), new String(a[0].getBytes(Constants.ISO_8859_1), enc)); //url request
} catch (/*UnsupportedEncoding*/Exception e1) {
parametersMap.put(e.getKey(), a[0]);
}
} else {
for (int i = 0; i < a.length; i++) {
try {
if (methodPost)
a[i] = URLDecoder.decode(a[i], enc);
else
a[i] = new String(a[i].getBytes(Constants.ISO_8859_1), enc);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
}
parametersMap.put(e.getKey(), a);
}
}
// Debug.debug("ParamsHelper Line:74.====== 結束 ==========.... Leave Out decodeRequestParameterMap()");
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -