📄 requestutils.java
字号:
public static Double getDoubleParameter(HttpServletRequest request, String name)
throws ServletRequestBindingException {
return ServletRequestUtils.getDoubleParameter(request, name);
}
/**
* Get a double parameter, with a fallback value. Never throws an exception.
* Can pass a distinguished value as default to enable checks of whether it was supplied.
* @param request current HTTP request
* @param name the name of the parameter
* @param defaultVal the default value to use as fallback
*/
public static double getDoubleParameter(HttpServletRequest request, String name, double defaultVal) {
return ServletRequestUtils.getDoubleParameter(request, name, defaultVal);
}
/**
* Get an array of double parameters, return an empty array if not found.
* @param request current HTTP request
* @param name the name of the parameter with multiple possible values
*/
public static double[] getDoubleParameters(HttpServletRequest request, String name) {
return ServletRequestUtils.getDoubleParameters(request, name);
}
/**
* Get a double parameter, throwing an exception if it isn't found or isn't a number.
* @param request current HTTP request
* @param name the name of the parameter
* @throws ServletRequestBindingException a subclass of ServletException,
* so it doesn't need to be caught
*/
public static double getRequiredDoubleParameter(HttpServletRequest request, String name)
throws ServletRequestBindingException {
return ServletRequestUtils.getRequiredDoubleParameter(request, name);
}
/**
* Get an array of double parameters, throwing an exception if not found or one is not a number.
* @param request current HTTP request
* @param name the name of the parameter with multiple possible values
* @throws ServletRequestBindingException a subclass of ServletException,
* so it doesn't need to be caught
*/
public static double[] getRequiredDoubleParameters(HttpServletRequest request, String name)
throws ServletRequestBindingException {
return ServletRequestUtils.getRequiredDoubleParameters(request, name);
}
/**
* Get a Boolean parameter, or <code>null</code> if not present.
* Throws an exception if it the parameter value isn't a boolean.
* <p>Accepts "true", "on", "yes" (any case) and "1" as values for true;
* treats every other non-empty value as false (i.e. parses leniently).
* @param request current HTTP request
* @param name the name of the parameter
* @return the Boolean value, or <code>null</code> if not present
* @throws ServletRequestBindingException a subclass of ServletException,
* so it doesn't need to be caught
*/
public static Boolean getBooleanParameter(HttpServletRequest request, String name)
throws ServletRequestBindingException {
if (request.getParameter(name) == null) {
return null;
}
return (getRequiredBooleanParameter(request, name) ? Boolean.TRUE : Boolean.FALSE);
}
/**
* Get a boolean parameter, with a fallback value. Never throws an exception.
* Can pass a distinguished value as default to enable checks of whether it was supplied.
* <p>Accepts "true", "on", "yes" (any case) and "1" as values for true;
* treats every other non-empty value as false (i.e. parses leniently).
* @param request current HTTP request
* @param name the name of the parameter
* @param defaultVal the default value to use as fallback
*/
public static boolean getBooleanParameter(HttpServletRequest request, String name, boolean defaultVal) {
if (request.getParameter(name) == null) {
return defaultVal;
}
try {
return getRequiredBooleanParameter(request, name);
}
catch (ServletRequestBindingException ex) {
return defaultVal;
}
}
/**
* Get an array of boolean parameters, return an empty array if not found.
* <p>Accepts "true", "on", "yes" (any case) and "1" as values for true;
* treats every other non-empty value as false (i.e. parses leniently).
* @param request current HTTP request
* @param name the name of the parameter with multiple possible values
*/
public static boolean[] getBooleanParameters(HttpServletRequest request, String name) {
try {
return getRequiredBooleanParameters(request, name);
}
catch (ServletRequestBindingException ex) {
return new boolean[0];
}
}
/**
* Get a boolean parameter, throwing an exception if it isn't found
* or isn't a boolean.
* <p>Accepts "true", "on", "yes" (any case) and "1" as values for true;
* treats every other non-empty value as false (i.e. parses leniently).
* @param request current HTTP request
* @param name the name of the parameter
* @throws ServletRequestBindingException a subclass of ServletException,
* so it doesn't need to be caught
*/
public static boolean getRequiredBooleanParameter(HttpServletRequest request, String name)
throws ServletRequestBindingException {
boolean value = ServletRequestUtils.getRequiredBooleanParameter(request, name);
if (!value && "".equals(request.getParameter(name))) {
throw new ServletRequestBindingException(
"Required boolean parameter '" + name + "' contains no value");
}
return value;
}
/**
* Get an array of boolean parameters, throwing an exception if not found
* or one isn't a boolean.
* <p>Accepts "true", "on", "yes" (any case) and "1" as values for true;
* treats every other non-empty value as false (i.e. parses leniently).
* @param request current HTTP request
* @param name the name of the parameter
* @throws ServletRequestBindingException a subclass of ServletException,
* so it doesn't need to be caught
*/
public static boolean[] getRequiredBooleanParameters(HttpServletRequest request, String name)
throws ServletRequestBindingException {
boolean[] values = ServletRequestUtils.getRequiredBooleanParameters(request, name);
for (int i = 0; i < values.length; i++) {
if (!values[i] && "".equals(request.getParameterValues(name)[i])) {
throw new ServletRequestBindingException(
"Required boolean parameter '" + name + "' contains no value");
}
}
return values;
}
/**
* Get a String parameter, or <code>null</code> if not present.
* Throws an exception if it the parameter value is empty.
* @param request current HTTP request
* @param name the name of the parameter
* @return the String value, or <code>null</code> if not present
* @throws ServletRequestBindingException a subclass of ServletException,
* so it doesn't need to be caught
*/
public static String getStringParameter(HttpServletRequest request, String name)
throws ServletRequestBindingException {
if (request.getParameter(name) == null) {
return null;
}
return getRequiredStringParameter(request, name);
}
/**
* Get a String parameter, with a fallback value. Never throws an exception.
* Can pass a distinguished value to default to enable checks of whether it was supplied.
* @param request current HTTP request
* @param name the name of the parameter
* @param defaultVal the default value to use as fallback
*/
public static String getStringParameter(HttpServletRequest request, String name, String defaultVal) {
if (request.getParameter(name) == null) {
return defaultVal;
}
try {
return getRequiredStringParameter(request, name);
}
catch (ServletRequestBindingException ex) {
return defaultVal;
}
}
/**
* Get an array of String parameters, return an empty array if not found.
* @param request current HTTP request
* @param name the name of the parameter with multiple possible values
*/
public static String[] getStringParameters(HttpServletRequest request, String name) {
try {
return getRequiredStringParameters(request, name);
}
catch (ServletRequestBindingException ex) {
return new String[0];
}
}
/**
* Get a String parameter, throwing an exception if it isn't found or is empty.
* @param request current HTTP request
* @param name the name of the parameter
* @throws ServletRequestBindingException a subclass of ServletException,
* so it doesn't need to be caught
*/
public static String getRequiredStringParameter(HttpServletRequest request, String name)
throws ServletRequestBindingException {
String value = ServletRequestUtils.getRequiredStringParameter(request, name);
if ("".equals(value)) {
throw new ServletRequestBindingException(
"Required string parameter '" + name + "' contains no value");
}
return value;
}
/**
* Get an array of String parameters, throwing an exception if not found or one is empty.
* @param request current HTTP request
* @param name the name of the parameter
* @throws ServletRequestBindingException a subclass of ServletException,
* so it doesn't need to be caught
*/
public static String[] getRequiredStringParameters(HttpServletRequest request, String name)
throws ServletRequestBindingException {
String[] values = ServletRequestUtils.getRequiredStringParameters(request, name);
for (int i = 0; i < values.length; i++) {
if ("".equals(values[i])) {
throw new ServletRequestBindingException(
"Required string parameter '" + name + "' contains no value");
}
}
return values;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -