一、前言
基于spring-web(4.1.4)包的org.springframework.web.bind.ServletRequestUtils工具类对HttpServletRequest获取的传递入参获取request.getParameter进行常见默认数据类型(整型IntParser、长整型LongParser、浮点数类型FloatParser、双精度浮点数、布尔型类型DoubleParser、字符串类型StringParser)的转换,具体代码如下所示
二、源码说明
package org.springframework.web.bind;@b@@b@import javax.servlet.ServletRequest;@b@@b@public abstract class ServletRequestUtils@b@{@b@ private static final IntParser INT_PARSER = new IntParser(null);@b@ private static final LongParser LONG_PARSER = new LongParser(null);@b@ private static final FloatParser FLOAT_PARSER = new FloatParser(null);@b@ private static final DoubleParser DOUBLE_PARSER = new DoubleParser(null);@b@ private static final BooleanParser BOOLEAN_PARSER = new BooleanParser(null);@b@ private static final StringParser STRING_PARSER = new StringParser(null);@b@@b@ public static Integer getIntParameter(ServletRequest request, String name)@b@ throws ServletRequestBindingException@b@ {@b@ if (request.getParameter(name) == null)@b@ return null;@b@@b@ return Integer.valueOf(getRequiredIntParameter(request, name));@b@ }@b@@b@ public static int getIntParameter(ServletRequest request, String name, int defaultVal)@b@ {@b@ if (request.getParameter(name) == null)@b@ return defaultVal;@b@ try@b@ {@b@ return getRequiredIntParameter(request, name);@b@ } catch (ServletRequestBindingException ex) {@b@ }@b@ return defaultVal;@b@ }@b@@b@ public static int[] getIntParameters(ServletRequest request, String name)@b@ {@b@ try@b@ {@b@ return getRequiredIntParameters(request, name);@b@ } catch (ServletRequestBindingException ex) {@b@ }@b@ return new int[0];@b@ }@b@@b@ public static int getRequiredIntParameter(ServletRequest request, String name)@b@ throws ServletRequestBindingException@b@ {@b@ return INT_PARSER.parseInt(name, request.getParameter(name));@b@ }@b@@b@ public static int[] getRequiredIntParameters(ServletRequest request, String name)@b@ throws ServletRequestBindingException@b@ {@b@ return INT_PARSER.parseInts(name, request.getParameterValues(name));@b@ }@b@@b@ public static Long getLongParameter(ServletRequest request, String name)@b@ throws ServletRequestBindingException@b@ {@b@ if (request.getParameter(name) == null)@b@ return null;@b@@b@ return Long.valueOf(getRequiredLongParameter(request, name));@b@ }@b@@b@ public static long getLongParameter(ServletRequest request, String name, long defaultVal)@b@ {@b@ if (request.getParameter(name) == null)@b@ return defaultVal;@b@ try@b@ {@b@ return getRequiredLongParameter(request, name);@b@ } catch (ServletRequestBindingException ex) {@b@ }@b@ return defaultVal;@b@ }@b@@b@ public static long[] getLongParameters(ServletRequest request, String name)@b@ {@b@ try@b@ {@b@ return getRequiredLongParameters(request, name);@b@ } catch (ServletRequestBindingException ex) {@b@ }@b@ return new long[0];@b@ }@b@@b@ public static long getRequiredLongParameter(ServletRequest request, String name)@b@ throws ServletRequestBindingException@b@ {@b@ return LONG_PARSER.parseLong(name, request.getParameter(name));@b@ }@b@@b@ public static long[] getRequiredLongParameters(ServletRequest request, String name)@b@ throws ServletRequestBindingException@b@ {@b@ return LONG_PARSER.parseLongs(name, request.getParameterValues(name));@b@ }@b@@b@ public static Float getFloatParameter(ServletRequest request, String name)@b@ throws ServletRequestBindingException@b@ {@b@ if (request.getParameter(name) == null)@b@ return null;@b@@b@ return Float.valueOf(getRequiredFloatParameter(request, name));@b@ }@b@@b@ public static float getFloatParameter(ServletRequest request, String name, float defaultVal)@b@ {@b@ if (request.getParameter(name) == null)@b@ return defaultVal;@b@ try@b@ {@b@ return getRequiredFloatParameter(request, name);@b@ } catch (ServletRequestBindingException ex) {@b@ }@b@ return defaultVal;@b@ }@b@@b@ public static float[] getFloatParameters(ServletRequest request, String name)@b@ {@b@ try@b@ {@b@ return getRequiredFloatParameters(request, name);@b@ } catch (ServletRequestBindingException ex) {@b@ }@b@ return new float[0];@b@ }@b@@b@ public static float getRequiredFloatParameter(ServletRequest request, String name)@b@ throws ServletRequestBindingException@b@ {@b@ return FLOAT_PARSER.parseFloat(name, request.getParameter(name));@b@ }@b@@b@ public static float[] getRequiredFloatParameters(ServletRequest request, String name)@b@ throws ServletRequestBindingException@b@ {@b@ return FLOAT_PARSER.parseFloats(name, request.getParameterValues(name));@b@ }@b@@b@ public static Double getDoubleParameter(ServletRequest request, String name)@b@ throws ServletRequestBindingException@b@ {@b@ if (request.getParameter(name) == null)@b@ return null;@b@@b@ return Double.valueOf(getRequiredDoubleParameter(request, name));@b@ }@b@@b@ public static double getDoubleParameter(ServletRequest request, String name, double defaultVal)@b@ {@b@ if (request.getParameter(name) == null)@b@ return defaultVal;@b@ try@b@ {@b@ return getRequiredDoubleParameter(request, name);@b@ } catch (ServletRequestBindingException ex) {@b@ }@b@ return defaultVal;@b@ }@b@@b@ public static double[] getDoubleParameters(ServletRequest request, String name)@b@ {@b@ try@b@ {@b@ return getRequiredDoubleParameters(request, name);@b@ } catch (ServletRequestBindingException ex) {@b@ }@b@ return new double[0];@b@ }@b@@b@ public static double getRequiredDoubleParameter(ServletRequest request, String name)@b@ throws ServletRequestBindingException@b@ {@b@ return DOUBLE_PARSER.parseDouble(name, request.getParameter(name));@b@ }@b@@b@ public static double[] getRequiredDoubleParameters(ServletRequest request, String name)@b@ throws ServletRequestBindingException@b@ {@b@ return DOUBLE_PARSER.parseDoubles(name, request.getParameterValues(name));@b@ }@b@@b@ public static Boolean getBooleanParameter(ServletRequest request, String name)@b@ throws ServletRequestBindingException@b@ {@b@ if (request.getParameter(name) == null)@b@ return null;@b@@b@ return Boolean.valueOf(getRequiredBooleanParameter(request, name));@b@ }@b@@b@ public static boolean getBooleanParameter(ServletRequest request, String name, boolean defaultVal)@b@ {@b@ if (request.getParameter(name) == null)@b@ return defaultVal;@b@ try@b@ {@b@ return getRequiredBooleanParameter(request, name);@b@ } catch (ServletRequestBindingException ex) {@b@ }@b@ return defaultVal;@b@ }@b@@b@ public static boolean[] getBooleanParameters(ServletRequest request, String name)@b@ {@b@ try@b@ {@b@ return getRequiredBooleanParameters(request, name);@b@ } catch (ServletRequestBindingException ex) {@b@ }@b@ return new boolean[0];@b@ }@b@@b@ public static boolean getRequiredBooleanParameter(ServletRequest request, String name)@b@ throws ServletRequestBindingException@b@ {@b@ return BOOLEAN_PARSER.parseBoolean(name, request.getParameter(name));@b@ }@b@@b@ public static boolean[] getRequiredBooleanParameters(ServletRequest request, String name)@b@ throws ServletRequestBindingException@b@ {@b@ return BOOLEAN_PARSER.parseBooleans(name, request.getParameterValues(name));@b@ }@b@@b@ public static String getStringParameter(ServletRequest request, String name)@b@ throws ServletRequestBindingException@b@ {@b@ if (request.getParameter(name) == null)@b@ return null;@b@@b@ return getRequiredStringParameter(request, name);@b@ }@b@@b@ public static String getStringParameter(ServletRequest request, String name, String defaultVal)@b@ {@b@ String val = request.getParameter(name);@b@ return ((val != null) ? val : defaultVal);@b@ }@b@@b@ public static String[] getStringParameters(ServletRequest request, String name)@b@ {@b@ try@b@ {@b@ return getRequiredStringParameters(request, name);@b@ } catch (ServletRequestBindingException ex) {@b@ }@b@ return new String[0];@b@ }@b@@b@ public static String getRequiredStringParameter(ServletRequest request, String name)@b@ throws ServletRequestBindingException@b@ {@b@ return STRING_PARSER.validateRequiredString(name, request.getParameter(name));@b@ }@b@@b@ public static String[] getRequiredStringParameters(ServletRequest request, String name)@b@ throws ServletRequestBindingException@b@ {@b@ return STRING_PARSER.validateRequiredStrings(name, request.getParameterValues(name));@b@ }@b@@b@ private static class StringParser extends ServletRequestUtils.ParameterParser<String>@b@ {@b@ private StringParser()@b@ {@b@ super(null);@b@ }@b@@b@ protected String getType() {@b@ return "string";@b@ }@b@@b@ protected String doParse(String parameter) throws NumberFormatException@b@ {@b@ return parameter;@b@ }@b@@b@ public String validateRequiredString(String name, String value) throws ServletRequestBindingException {@b@ validateRequiredParameter(name, value);@b@ return value;@b@ }@b@@b@ public String[] validateRequiredStrings(String name, String[] values) throws ServletRequestBindingException {@b@ validateRequiredParameter(name, values);@b@ String[] arrayOfString = values; int i = arrayOfString.length; for (int j = 0; j < i; ++j) { String value = arrayOfString[j];@b@ validateRequiredParameter(name, value);@b@ }@b@ return values;@b@ }@b@ }@b@@b@ private static class BooleanParser extends ServletRequestUtils.ParameterParser<Boolean>@b@ {@b@ private BooleanParser()@b@ {@b@ super(null);@b@ }@b@@b@ protected String getType() {@b@ return "boolean";@b@ }@b@@b@ protected Boolean doParse(String parameter) throws NumberFormatException@b@ {@b@ return Boolean.valueOf((parameter.equalsIgnoreCase("true")) || (parameter.equalsIgnoreCase("on")) || @b@ (parameter@b@ .equalsIgnoreCase("yes")) || @b@ (parameter.equals("1")));@b@ }@b@@b@ public boolean parseBoolean(String name, String parameter) throws ServletRequestBindingException {@b@ return ((Boolean)parse(name, parameter)).booleanValue();@b@ }@b@@b@ public boolean[] parseBooleans(String name, String[] values) throws ServletRequestBindingException {@b@ validateRequiredParameter(name, values);@b@ boolean[] parameters = new boolean[values.length];@b@ for (int i = 0; i < values.length; ++i)@b@ parameters[i] = parseBoolean(name, values[i]);@b@@b@ return parameters;@b@ }@b@ }@b@@b@ private static class DoubleParser extends ServletRequestUtils.ParameterParser<Double>@b@ {@b@ private DoubleParser()@b@ {@b@ super(null);@b@ }@b@@b@ protected String getType() {@b@ return "double";@b@ }@b@@b@ protected Double doParse(String parameter) throws NumberFormatException@b@ {@b@ return Double.valueOf(parameter);@b@ }@b@@b@ public double parseDouble(String name, String parameter) throws ServletRequestBindingException {@b@ return ((Double)parse(name, parameter)).doubleValue();@b@ }@b@@b@ public double[] parseDoubles(String name, String[] values) throws ServletRequestBindingException {@b@ validateRequiredParameter(name, values);@b@ double[] parameters = new double[values.length];@b@ for (int i = 0; i < values.length; ++i)@b@ parameters[i] = parseDouble(name, values[i]);@b@@b@ return parameters;@b@ }@b@ }@b@@b@ private static class FloatParser extends ServletRequestUtils.ParameterParser<Float>@b@ {@b@ private FloatParser()@b@ {@b@ super(null);@b@ }@b@@b@ protected String getType() {@b@ return "float";@b@ }@b@@b@ protected Float doParse(String parameter) throws NumberFormatException@b@ {@b@ return Float.valueOf(parameter);@b@ }@b@@b@ public float parseFloat(String name, String parameter) throws ServletRequestBindingException {@b@ return ((Float)parse(name, parameter)).floatValue();@b@ }@b@@b@ public float[] parseFloats(String name, String[] values) throws ServletRequestBindingException {@b@ validateRequiredParameter(name, values);@b@ float[] parameters = new float[values.length];@b@ for (int i = 0; i < values.length; ++i)@b@ parameters[i] = parseFloat(name, values[i]);@b@@b@ return parameters;@b@ }@b@ }@b@@b@ private static class LongParser extends ServletRequestUtils.ParameterParser<Long>@b@ {@b@ private LongParser()@b@ {@b@ super(null);@b@ }@b@@b@ protected String getType() {@b@ return "long";@b@ }@b@@b@ protected Long doParse(String parameter) throws NumberFormatException@b@ {@b@ return Long.valueOf(parameter);@b@ }@b@@b@ public long parseLong(String name, String parameter) throws ServletRequestBindingException {@b@ return ((Long)parse(name, parameter)).longValue();@b@ }@b@@b@ public long[] parseLongs(String name, String[] values) throws ServletRequestBindingException {@b@ validateRequiredParameter(name, values);@b@ long[] parameters = new long[values.length];@b@ for (int i = 0; i < values.length; ++i)@b@ parameters[i] = parseLong(name, values[i]);@b@@b@ return parameters;@b@ }@b@ }@b@@b@ private static class IntParser extends ServletRequestUtils.ParameterParser<Integer>@b@ {@b@ private IntParser()@b@ {@b@ super(null);@b@ }@b@@b@ protected String getType() {@b@ return "int";@b@ }@b@@b@ protected Integer doParse(String s) throws NumberFormatException@b@ {@b@ return Integer.valueOf(s);@b@ }@b@@b@ public int parseInt(String name, String parameter) throws ServletRequestBindingException {@b@ return ((Integer)parse(name, parameter)).intValue();@b@ }@b@@b@ public int[] parseInts(String name, String[] values) throws ServletRequestBindingException {@b@ validateRequiredParameter(name, values);@b@ int[] parameters = new int[values.length];@b@ for (int i = 0; i < values.length; ++i)@b@ parameters[i] = parseInt(name, values[i]);@b@@b@ return parameters;@b@ }@b@ }@b@@b@ private static abstract class ParameterParser<T>@b@ {@b@ protected final T parse(String name, String parameter)@b@ throws ServletRequestBindingException@b@ {@b@ validateRequiredParameter(name, parameter);@b@ try {@b@ return doParse(parameter);@b@ }@b@ catch (NumberFormatException ex)@b@ {@b@ throw new ServletRequestBindingException("Required " + @b@ getType() + " parameter '" + name + "' with value of '" + parameter + "' is not a valid number", ex);@b@ }@b@ }@b@@b@ protected final void validateRequiredParameter(String name, Object parameter)@b@ throws ServletRequestBindingException@b@ {@b@ if (parameter == null)@b@ throw new MissingServletRequestParameterException(name, getType());@b@ }@b@@b@ protected abstract String getType();@b@@b@ protected abstract T doParse(String paramString)@b@ throws NumberFormatException;@b@ }@b@}