关于通过spring-web的ServletRequestUtils工具类对获取getParameter传参的默认转换基本数据类型的学习
基于spring-web(4.1.4)包的org.springframework.web.bind.ServletRequestUtils工具类对HttpServletRequest获取的传递入参获取request.getParameter进行常见默认数据类型(整型IntParser、长整型LongParser、浮点数类型FloatParser、双精度浮点数、布尔型类型DoubleParser、字符串类型StringParser)的转换,源码如下所示
package org.springframework.web.bind; import javax.servlet.ServletRequest; public abstract class ServletRequestUtils
{
private static final IntParser INT_PARSER = new IntParser(null);
private static final LongParser LONG_PARSER = new LongParser(null);
private static final FloatParser FLOAT_PARSER = new FloatParser(null);
private static final DoubleParser DOUBLE_PARSER = new DoubleParser(null);
private static final BooleanParser BOOLEAN_PARSER = new BooleanParser(null);
private static final StringParser STRING_PARSER = new StringParser(null); public static Integer getIntParameter(ServletRequest request, String name)
throws ServletRequestBindingException
{
if (request.getParameter(name) == null)
return null; return Integer.valueOf(getRequiredIntParameter(request, name));
} public static int getIntParameter(ServletRequest request, String name, int defaultVal)
{
if (request.getParameter(name) == null)
return defaultVal;
try
{
return getRequiredIntParameter(request, name);
} catch (ServletRequestBindingException ex) {
}
return defaultVal;
} public static int[] getIntParameters(ServletRequest request, String name)
{
try
{
return getRequiredIntParameters(request, name);
} catch (ServletRequestBindingException ex) {
}
return new int[0];
} public static int getRequiredIntParameter(ServletRequest request, String name)
throws ServletRequestBindingException
{
return INT_PARSER.parseInt(name, request.getParameter(name));
} public static int[] getRequiredIntParameters(ServletRequest request, String name)
throws ServletRequestBindingException
{
return INT_PARSER.parseInts(name, request.getParameterValues(name));
} public static Long getLongParameter(ServletRequest request, String name)
throws ServletRequestBindingException
{
if (request.getParameter(name) == null)
return null; return Long.valueOf(getRequiredLongParameter(request, name));
} public static long getLongParameter(ServletRequest request, String name, long defaultVal)
{
if (request.getParameter(name) == null)
return defaultVal;
try
{
return getRequiredLongParameter(request, name);
} catch (ServletRequestBindingException ex) {
}
return defaultVal;
} public static long[] getLongParameters(ServletRequest request, String name)
{
try
{
return getRequiredLongParameters(request, name);
} catch (ServletRequestBindingException ex) {
}
return new long[0];
} public static long getRequiredLongParameter(ServletRequest request, String name)
throws ServletRequestBindingException
{
return LONG_PARSER.parseLong(name, request.getParameter(name));
} public static long[] getRequiredLongParameters(ServletRequest request, String name)
throws ServletRequestBindingException
{
return LONG_PARSER.parseLongs(name, request.getParameterValues(name));
} public static Float getFloatParameter(ServletRequest request, String name)
throws ServletRequestBindingException
{
if (request.getParameter(name) == null)
return null; return Float.valueOf(getRequiredFloatParameter(request, name));
} public static float getFloatParameter(ServletRequest request, String name, float defaultVal)
{
if (request.getParameter(name) == null)
return defaultVal;
try
{
return getRequiredFloatParameter(request, name);
} catch (ServletRequestBindingException ex) {
}
return defaultVal;
} public static float[] getFloatParameters(ServletRequest request, String name)
{
try
{
return getRequiredFloatParameters(request, name);
} catch (ServletRequestBindingException ex) {
}
return new float[0];
} public static float getRequiredFloatParameter(ServletRequest request, String name)
throws ServletRequestBindingException
{
return FLOAT_PARSER.parseFloat(name, request.getParameter(name));
} public static float[] getRequiredFloatParameters(ServletRequest request, String name)
throws ServletRequestBindingException
{
return FLOAT_PARSER.parseFloats(name, request.getParameterValues(name));
} public static Double getDoubleParameter(ServletRequest request, String name)
throws ServletRequestBindingException
{
if (request.getParameter(name) == null)
return null; return Double.valueOf(getRequiredDoubleParameter(request, name));
} public static double getDoubleParameter(ServletRequest request, String name, double defaultVal)
{
if (request.getParameter(name) == null)
return defaultVal;
try
{
return getRequiredDoubleParameter(request, name);
} catch (ServletRequestBindingException ex) {
}
return defaultVal;
} public static double[] getDoubleParameters(ServletRequest request, String name)
{
try
{
return getRequiredDoubleParameters(request, name);
} catch (ServletRequestBindingException ex) {
}
return new double[0];
} public static double getRequiredDoubleParameter(ServletRequest request, String name)
throws ServletRequestBindingException
{
return DOUBLE_PARSER.parseDouble(name, request.getParameter(name));
} public static double[] getRequiredDoubleParameters(ServletRequest request, String name)
throws ServletRequestBindingException
{
return DOUBLE_PARSER.parseDoubles(name, request.getParameterValues(name));
} public static Boolean getBooleanParameter(ServletRequest request, String name)
throws ServletRequestBindingException
{
if (request.getParameter(name) == null)
return null; return Boolean.valueOf(getRequiredBooleanParameter(request, name));
} public static boolean getBooleanParameter(ServletRequest request, String name, boolean defaultVal)
{
if (request.getParameter(name) == null)
return defaultVal;
try
{
return getRequiredBooleanParameter(request, name);
} catch (ServletRequestBindingException ex) {
}
return defaultVal;
} public static boolean[] getBooleanParameters(ServletRequest request, String name)
{
try
{
return getRequiredBooleanParameters(request, name);
} catch (ServletRequestBindingException ex) {
}
return new boolean[0];
} public static boolean getRequiredBooleanParameter(ServletRequest request, String name)
throws ServletRequestBindingException
{
return BOOLEAN_PARSER.parseBoolean(name, request.getParameter(name));
} public static boolean[] getRequiredBooleanParameters(ServletRequest request, String name)
throws ServletRequestBindingException
{
return BOOLEAN_PARSER.parseBooleans(name, request.getParameterValues(name));
} public static String getStringParameter(ServletRequest request, String name)
throws ServletRequestBindingException
{
if (request.getParameter(name) == null)
return null; return getRequiredStringParameter(request, name);
} public static String getStringParameter(ServletRequest request, String name, String defaultVal)
{
String val = request.getParameter(name);
return ((val != null) ? val : defaultVal);
} public static String[] getStringParameters(ServletRequest request, String name)
{
try
{
return getRequiredStringParameters(request, name);
} catch (ServletRequestBindingException ex) {
}
return new String[0];
} public static String getRequiredStringParameter(ServletRequest request, String name)
throws ServletRequestBindingException
{
return STRING_PARSER.validateRequiredString(name, request.getParameter(name));
} public static String[] getRequiredStringParameters(ServletRequest request, String name)
throws ServletRequestBindingException
{
return STRING_PARSER.validateRequiredStrings(name, request.getParameterValues(name));
} private static class StringParser extends ServletRequestUtils.ParameterParser<String>
{
private StringParser()
{
super(null);
} protected String getType() {
return "string";
} protected String doParse(String parameter) throws NumberFormatException
{
return parameter;
} public String validateRequiredString(String name, String value) throws ServletRequestBindingException {
validateRequiredParameter(name, value);
return value;
} public String[] validateRequiredStrings(String name, String[] values) throws ServletRequestBindingException {
validateRequiredParameter(name, values);
String[] arrayOfString = values; int i = arrayOfString.length; for (int j = 0; j < i; ++j) { String value = arrayOfString[j];
validateRequiredParameter(name, value);
}
return values;
}
} private static class BooleanParser extends ServletRequestUtils.ParameterParser<Boolean>
{
private BooleanParser()
{
super(null);
} protected String getType() {
return "boolean";
} protected Boolean doParse(String parameter) throws NumberFormatException
{
return Boolean.valueOf((parameter.equalsIgnoreCase("true")) || (parameter.equalsIgnoreCase("on")) ||
(parameter
.equalsIgnoreCase("yes")) ||
(parameter.equals("1")));
} public boolean parseBoolean(String name, String parameter) throws ServletRequestBindingException {
return ((Boolean)parse(name, parameter)).booleanValue();
} public boolean[] parseBooleans(String name, String[] values) throws ServletRequestBindingException {
validateRequiredParameter(name, values);
boolean[] parameters = new boolean[values.length];
for (int i = 0; i < values.length; ++i)
parameters[i] = parseBoolean(name, values[i]); return parameters;
}
} private static class DoubleParser extends ServletRequestUtils.ParameterParser<Double>
{
private DoubleParser()
{
super(null);
} protected String getType() {
return "double";
} protected Double doParse(String parameter) throws NumberFormatException
{
return Double.valueOf(parameter);
} public double parseDouble(String name, String parameter) throws ServletRequestBindingException {
return ((Double)parse(name, parameter)).doubleValue();
} public double[] parseDoubles(String name, String[] values) throws ServletRequestBindingException {
validateRequiredParameter(name, values);
double[] parameters = new double[values.length];
for (int i = 0; i < values.length; ++i)
parameters[i] = parseDouble(name, values[i]); return parameters;
}
} private static class FloatParser extends ServletRequestUtils.ParameterParser<Float>
{
private FloatParser()
{
super(null);
} protected String getType() {
return "float";
} protected Float doParse(String parameter) throws NumberFormatException
{
return Float.valueOf(parameter);
} public float parseFloat(String name, String parameter) throws ServletRequestBindingException {
return ((Float)parse(name, parameter)).floatValue();
} public float[] parseFloats(String name, String[] values) throws ServletRequestBindingException {
validateRequiredParameter(name, values);
float[] parameters = new float[values.length];
for (int i = 0; i < values.length; ++i)
parameters[i] = parseFloat(name, values[i]); return parameters;
}
} private static class LongParser extends ServletRequestUtils.ParameterParser<Long>
{
private LongParser()
{
super(null);
} protected String getType() {
return "long";
} protected Long doParse(String parameter) throws NumberFormatException
{
return Long.valueOf(parameter);
} public long parseLong(String name, String parameter) throws ServletRequestBindingException {
return ((Long)parse(name, parameter)).longValue();
} public long[] parseLongs(String name, String[] values) throws ServletRequestBindingException {
validateRequiredParameter(name, values);
long[] parameters = new long[values.length];
for (int i = 0; i < values.length; ++i)
parameters[i] = parseLong(name, values[i]); return parameters;
}
} private static class IntParser extends ServletRequestUtils.ParameterParser<Integer>
{
private IntParser()
{
super(null);
} protected String getType() {
return "int";
} protected Integer doParse(String s) throws NumberFormatException
{
return Integer.valueOf(s);
} public int parseInt(String name, String parameter) throws ServletRequestBindingException {
return ((Integer)parse(name, parameter)).intValue();
} public int[] parseInts(String name, String[] values) throws ServletRequestBindingException {
validateRequiredParameter(name, values);
int[] parameters = new int[values.length];
for (int i = 0; i < values.length; ++i)
parameters[i] = parseInt(name, values[i]); return parameters;
}
} private static abstract class ParameterParser<T>
{
protected final T parse(String name, String parameter)
throws ServletRequestBindingException
{
validateRequiredParameter(name, parameter);
try {
return doParse(parameter);
}
catch (NumberFormatException ex)
{
throw new ServletRequestBindingException("Required " +
getType() + " parameter '" + name + "' with value of '" + parameter + "' is not a valid number", ex);
}
} protected final void validateRequiredParameter(String name, Object parameter)
throws ServletRequestBindingException
{
if (parameter == null)
throw new MissingServletRequestParameterException(name, getType());
} protected abstract String getType(); protected abstract T doParse(String paramString)
throws NumberFormatException;
}
}
关于通过spring-web的ServletRequestUtils工具类对获取getParameter传参的默认转换基本数据类型的学习的更多相关文章
- Spring统一返回Json工具类,带分页信息
前言: 项目做前后端分离时,我们会经常提供Json数据给前端,如果有一个统一的Json格式返回工具类,那么将大大提高开发效率和减低沟通成本. 此Json响应工具类,支持带分页信息,支持泛型,支持Htt ...
- 获取Spring容器Bean对象工具类
在开发中,总是能碰到用注解注入不了Spring容器里面bean对象的问题.为了解决这个问题,我们需要一个工具类来直接获取Spring容器中的bean.因此就写了这个工具类,在此记录一下,方便后续查阅. ...
- 使用scanner工具类来获取用户输入的信息
使用scanner工具类来获取用户输入的成绩信息. 步骤:1.导入java.util.Scanner包 2.创建Scanner对象 3.接受并保存用户输入的值 例子:通过用户输入来获取学生成绩 pac ...
- java Http消息传递之POST和GET两种方法--通过实用工具类来获取服务器资源
实现该方法需要导入一些jar包 可以去一下地址下载: http://pan.baidu.com/s/1hqrJF7m /** * 实用工具类来获取服务器资源 * * get方法传送数据 * * 1.通 ...
- Java 工具类 IpUtil - 获取本机所有 IP 地址,LocalHost 对应地址 IP
Java 工具类 IpUtil - 获取本机所有 IP 地址,LocalHost 对应地址 IP IP 工具类 源代码: /** * <p> * * @author XiaoPengwei ...
- Spring 常用的一些工具类
学习Java的人,或者开发很多项目,都需要使用到Spring 这个框架,这个框架对于java程序员来说.学好spring 就不怕找不到工作.我们时常会写一些工具类,但是有些时候 我们不清楚,我们些的工 ...
- Maven基础&&Spring框架阶段常用工具类整理
常用工具类 1.密码加密工具类: package com.itheima.utils; import java.security.MessageDigest; import sun.misc.BASE ...
- 获取spring上下文的bean 工具类
有些场景我们不属于controller,service,dao,但是我们需要从spring中得到spring容器里面的bean.这时候我们需要一个类继承 ApplicationContextAware ...
- Spring boot中普通工具类不能使用@Value注入yml文件中的自定义参数的问题
在写一个工具类的时候,因为要用到yml中的自定义参数,使用@Value发现值不能正常注入,都显示为null: yml文件中的自定义格式 调用工具类的时候不能new的方式 要使用@Autowired的方 ...
随机推荐
- linux中用anaconda使用不同版本python
1.使用命令conda create --name python36 python=3.6 #你想使用哪个版本就下载哪个版本,--name后面跟的是该虚拟环境的名称 2.需要使用python3.6时 ...
- (原创)EasyUI中datagrid的行编辑模式中,找到特定的Editor,并为其添加事件
有时候在行编辑的时候,一个编辑框的值要根据其它编辑框的值进行变化,那么可以通过在开启编辑时,找到特定的Editor,为其添加事件 // 绑定事件, index为当前编辑行 var editors = ...
- 2016/3/18 ①PHP基础 ② PHP函数 ③其他函数(随机数、关于日期) ④正则表达式 ⑤字符串处理
一.PHP基础 1,标记和注释 ①<?php?> ②单行注释// 多行注释/** */2, 输出语句 ①echo输出 echo可以输出多个字符串,用逗号隔开. ②print输出 pr ...
- hadoop第一个例子WordCount
hadoop查看自己空间 http://127.0.0.1:50070/dfshealth.jsp import java.io.IOException; import java.util.Strin ...
- AndroidEventBus总结
什么是AndroidEventBus? android事件总线,是一个发布 / 订阅的事件总线 github地址:https://github.com/greenrobot/EventBus Andr ...
- YTU 2887: D--机器人Bill
2887: D--机器人Bill 时间限制: 1 Sec 内存限制: 128 MB 提交: 112 解决: 22 题目描述 最近,Dr. Jiang 新设计一个机器人Bill.Bill很聪明,会做 ...
- mac系统下配置域名映射关系
1.cd /private/etc/ 先修改权限:sudo chmod 777 hosts vi hosts localhost:etc mhx$ cat hosts ## # Host Databa ...
- Windows下手动安装redis服务
1.Redis本身不支持windows,但是有另外的团队在维护着一个windows下的版本 2.下载地址: https://github.com/MSOpenTech/redis 2.8的版本在源 ...
- VS2013文件同步插件开发
一.插件功能描述 插件监控一个xml文件,当该文档有添加新元素在保存的时候将新增的元素同步到指定的目录下. 二.模板的选择 由于该功能是跟代码编辑有关的,要监控文档的保存事件,所以要在文档打开的时候就 ...
- 关于eclipse部署项目后,在tomcat中的webapps文件夹下没有项目
转自:https://blog.csdn.net/yang505581644/article/details/78802316 一.发现问题 在eclipse中新建Dynamic Web Projec ...