📄 webutils.java
字号:
package eols.tools.binder;
/**
* 该类重写了Spring框架提供的WebUtils类的getParametersStartingWith方法. 主要是为了提供额外的
* request中文参数值的编码转换功能.
*/
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Enumeration;
import java.util.List;
import javax.servlet.ServletRequest;
import eols.tools.ChineseSwitcher;
public class WebUtils {
/**
* Return a map containing all parameters with the given prefix.
* Maps single values to String and multiple values to String array.
* <p>For example, with a prefix of "spring_", "spring_param1" and
* "spring_param2" result in a Map with "param1" and "param2" as keys.
* <p>Similar to Servlet 2.3's <code>ServletRequest.getParameterMap</code>,
* but more flexible and compatible with Servlet 2.2.
* @param request HTTP request in which to look for parameters
* @param prefix the beginning of parameter names
* (if this is null or the empty string, all parameters will match)
* @return map containing request parameters <b>without the prefix</b>,
* containing either a String or a String array as values
* @see javax.servlet.ServletRequest#getParameterNames
* @see javax.servlet.ServletRequest#getParameterValues
* @see javax.servlet.ServletRequest#getParameterMap
*/
public static Map getParametersStartingWith(ServletRequest request, String prefix, boolean isResultNeedTranslate) {
Enumeration enum = request.getParameterNames();
Map params = new HashMap();
if (prefix == null) {
prefix = "";
}
while (enum != null && enum.hasMoreElements()) {
String paramName = (String) enum.nextElement();
if ("".equals(prefix) || paramName.startsWith(prefix)) {
String unprefixed = paramName.substring(prefix.length());
String[] values = request.getParameterValues(paramName);
if (values == null) {
// do nothing, no values found at all
}
//else if (values.length > 1) {
//params.put(unprefixed, ChineseSwitcher.toStore(values));
//params.put(unprefixed, values);
//}
else {
if (isResultNeedTranslate){
params.put(unprefixed,
ChineseSwitcher.
ISO8859TOGB2312(values[0]));
}
else {
params.put(unprefixed, values[0]);
}
//params.put(unprefixed, values[0]);
}
}
}
return params;
}
/**
* Return a map containing all needed parameters with the given prefix.
* Maps multiple values with the given name to String array.
*
* This method is mainly used to values of multiple bean properties. For example,
* suppose a client submits a form with many rows of datas, also suppose we want to
* fetch all these datas in one step, to realize this aim, we should first get all
* the request parameters(that is, form datas), then we fetch one row a time to instantialize
* and set property values of the given bean class, and add the result bean object into
* a List(ArrayList), finally, we return such a List to request controllers who need
* this object arraylist.
*
* @param request HTTP request in which to look for parameters
* @param parametersName parameters' name whose values are needed to be fetched
* @return map containing request parameters <b>without the prefix</b>,
* containing either a String or a String array as values
*
* @see javax.servlet.ServletRequest#getParameterNames
* @see javax.servlet.ServletRequest#getParameterValues
* @see javax.servlet.ServletRequest#getParameterMap
*/
public static Map getSpecifiedParameterValues(ServletRequest request, String[] parametersName) {
Map result = new HashMap();
// First, check whether the given parameters are valid
if (parametersName == null || parametersName.length == 0) {
return result;
}
// Second, fetch values of the specified parameter name
int size = parametersName.length;
// Variable indicating the row count of the datas submitted by the client
int rowCount = 0;
String paramName = null;
String[] values = null;
for (int i = 0; i < size; i++) {
paramName = parametersName[i];
values = request.getParameterValues(paramName);
if (values != null && values.length > rowCount) {
rowCount = values.length;
}
result.put(paramName, values);
}
result.put("rowCount", new Integer(rowCount));
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -