📄 requestutils.java
字号:
/*
* Copyright 2002-2004 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.bind;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
/**
* Parameter extraction methods, for an approach distinct from data binding,
* in which parameters of specific types are required.
*
* <p>This approach is very useful for simple submissions, where binding
* request parameters to a command object would be overkill.
*
* @author Rod Johnson
* @author Juergen Hoeller
* @author Keith Donald
*/
public abstract class RequestUtils {
private static final IntParser INT_PARSER = new IntParser();
private static final LongParser LONG_PARSER = new LongParser();
private static final FloatParser FLOAT_PARSER = new FloatParser();
private static final DoubleParser DOUBLE_PARSER = new DoubleParser();
private static final BooleanParser BOOLEAN_PARSER = new BooleanParser();
private static final StringParser STRING_PARSER = new StringParser();
/**
* Throw a ServletException if the given HTTP request method should be rejected.
* @param request request to check
* @param method method (such as "GET") which should be rejected
* @throws ServletException if the given HTTP request is rejected
*/
public static void rejectRequestMethod(HttpServletRequest request, String method) throws ServletException {
if (request.getMethod().equals(method)) {
throw new ServletException("This resource does not support request method '" + method + "'");
}
}
/**
* Get an Integer parameter, or null if not present.
* Throws an exception if it the parameter value isn't a number.
* @param request current HTTP request
* @param name the name of the parameter
* @return the Integer value, or null if not present
* @throws ServletRequestBindingException: subclass of ServletException,
* so it doesn't need to be caught
*/
public static Integer getIntParameter(HttpServletRequest request, String name)
throws ServletRequestBindingException {
if (request.getParameter(name) == null) {
return null;
}
return new Integer(getRequiredIntParameter(request, name));
}
/**
* Get an int 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 int getIntParameter(HttpServletRequest request, String name, int defaultVal) {
try {
return getRequiredIntParameter(request, name);
}
catch (ServletRequestBindingException ex) {
return defaultVal;
}
}
/**
* Get an array of int 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 int[] getIntParameters(HttpServletRequest request, String name) {
try {
return getRequiredIntParameters(request, name);
}
catch (ServletRequestBindingException ex) {
return new int[0];
}
}
/**
* Get an int 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: subclass of ServletException,
* so it doesn't need to be caught
*/
public static int getRequiredIntParameter(HttpServletRequest request, String name)
throws ServletRequestBindingException {
return INT_PARSER.parseInt(name, request.getParameter(name));
}
/**
* Get an array of int 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: subclass of ServletException,
* so it doesn't need to be caught
*/
public static int[] getRequiredIntParameters(HttpServletRequest request, String name)
throws ServletRequestBindingException {
return INT_PARSER.parseInts(name, request.getParameterValues(name));
}
/**
* Get a Long parameter, or null if not present.
* Throws an exception if it the parameter value isn't a number.
* @param request current HTTP request
* @param name the name of the parameter
* @return the Long value, or null if not present
* @throws ServletRequestBindingException: subclass of ServletException,
* so it doesn't need to be caught
*/
public static Long getLongParameter(HttpServletRequest request, String name)
throws ServletRequestBindingException {
if (request.getParameter(name) == null) {
return null;
}
return new Long(getRequiredLongParameter(request, name));
}
/**
* Get an int 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 long getLongParameter(HttpServletRequest request, String name, long defaultVal) {
try {
return getRequiredLongParameter(request, name);
}
catch (ServletRequestBindingException ex) {
return defaultVal;
}
}
/**
* Get an array of long 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 long[] getLongParameters(HttpServletRequest request, String name) {
try {
return getRequiredLongParameters(request, name);
}
catch (ServletRequestBindingException ex) {
return new long[0];
}
}
/**
* Get a long 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: subclass of ServletException,
* so it doesn't need to be caught
*/
public static long getRequiredLongParameter(HttpServletRequest request, String name)
throws ServletRequestBindingException {
return LONG_PARSER.parseLong(name, request.getParameter(name));
}
/**
* Get an array of long 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: subclass of ServletException,
* so it doesn't need to be caught
*/
public static long[] getRequiredLongParameters(HttpServletRequest request, String name)
throws ServletRequestBindingException {
return LONG_PARSER.parseLongs(name, request.getParameterValues(name));
}
/**
* Get a Float parameter, or null if not present.
* Throws an exception if it the parameter value isn't a number.
* @param request current HTTP request
* @param name the name of the parameter
* @return the Float value, or null if not present
* @throws ServletRequestBindingException: subclass of ServletException,
* so it doesn't need to be caught
*/
public static Float getFloatParameter(HttpServletRequest request, String name)
throws ServletRequestBindingException {
if (request.getParameter(name) == null) {
return null;
}
return new Float(getRequiredFloatParameter(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 float getFloatParameter(HttpServletRequest request, String name, float defaultVal) {
try {
return getRequiredFloatParameter(request, name);
}
catch (ServletRequestBindingException ex) {
return defaultVal;
}
}
/**
* Get an array of float 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 float[] getFloatParameters(HttpServletRequest request, String name) {
try {
return getRequiredFloatParameters(request, name);
}
catch (ServletRequestBindingException ex) {
return new float[0];
}
}
/**
* 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: subclass of ServletException,
* so it doesn't need to be caught
*/
public static float getRequiredFloatParameter(HttpServletRequest request, String name)
throws ServletRequestBindingException {
return FLOAT_PARSER.parseFloat(name, request.getParameter(name));
}
/**
* Get an array of float 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: subclass of ServletException,
* so it doesn't need to be caught
*/
public static float[] getRequiredFloatParameters(HttpServletRequest request, String name)
throws ServletRequestBindingException {
return FLOAT_PARSER.parseFloats(name, request.getParameterValues(name));
}
/**
* Get a Double parameter, or null if not present.
* Throws an exception if it the parameter value isn't a number.
* @param request current HTTP request
* @param name the name of the parameter
* @return the Double value, or null if not present
* @throws ServletRequestBindingException: subclass of ServletException,
* so it doesn't need to be caught
*/
public static Double getDoubleParameter(HttpServletRequest request, String name)
throws ServletRequestBindingException {
if (request.getParameter(name) == null) {
return null;
}
return new Double(getRequiredDoubleParameter(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) {
try {
return getRequiredDoubleParameter(request, name);
}
catch (ServletRequestBindingException ex) {
return defaultVal;
}
}
/**
* Get an array of float 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) {
try {
return getRequiredDoubleParameters(request, name);
}
catch (ServletRequestBindingException ex) {
return new double[0];
}
}
/**
* 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: subclass of ServletException,
* so it doesn't need to be caught
*/
public static double getRequiredDoubleParameter(HttpServletRequest request, String name)
throws ServletRequestBindingException {
return DOUBLE_PARSER.parseDouble(name, request.getParameter(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: subclass of ServletException,
* so it doesn't need to be caught
*/
public static double[] getRequiredDoubleParameters(HttpServletRequest request, String name)
throws ServletRequestBindingException {
return DOUBLE_PARSER.parseDoubles(name, request.getParameterValues(name));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -