WebUtils根据名称,知道是Spring当中一个工具类,主要用于Web应用程序,供各种框架使用。
其中有些方法还是挺有用的,比如可以获取Session中的会话属性,获取Cookies,设置Session中的会话属性的值等等
翻译了WebUtils类的文档,其中WebUtils中的方法如下:
1.将一个系统性质设置到上下文根路径
public static void setWebAppRootSystemProperty(ServletContext servletContext) throws IllegalStateException { Assert.notNull(servletContext, "ServletContext must not be null"); String root = servletContext.getRealPath("/"); if (root == null) { throw new IllegalStateException( "Cannot set web app root system property when WAR file is not expanded"); } String param = servletContext.getInitParameter(WEB_APP_ROOT_KEY_PARAM); String key = (param != null ? param : DEFAULT_WEB_APP_ROOT_KEY); String oldValue = System.getProperty(key); if (oldValue != null && !StringUtils.pathEquals(oldValue, root)) { throw new IllegalStateException( "Web app root system property already set to different value: '" + key + "' = [" + oldValue + "] instead of [" + root + "] - " + "Choose unique values for the 'webAppRootKey' context-param in your web.xml files!"); } System.setProperty(key, root); servletContext.log("Set web app root system property: '" + key + "' = [" + root + "]"); }
2.移除系统性质
public static void removeWebAppRootSystemProperty(ServletContext servletContext) { Assert.notNull(servletContext, "ServletContext must not be null"); String param = servletContext.getInitParameter(WEB_APP_ROOT_KEY_PARAM); String key = (param != null ? param : DEFAULT_WEB_APP_ROOT_KEY); System.getProperties().remove(key); }
3.判断“HTML escaping”(HTML转义)对应用是否允许,即看web.xml中的defaultHtmlEscape的值是否设置为true
public static Boolean getDefaultHtmlEscape(ServletContext servletContext) { if (servletContext == null) { return null; } String param = servletContext.getInitParameter(HTML_ESCAPE_CONTEXT_PARAM); return (StringUtils.hasText(param)? Boolean.valueOf(param) : null); }
4.返回由当前servlet容器提供的 当前Web应用程序的临时目录
public static File getTempDir(ServletContext servletContext) { Assert.notNull(servletContext, "ServletContext must not be null"); return (File) servletContext.getAttribute(TEMP_DIR_CONTEXT_ATTRIBUTE); }
5.返回由servlet容器提供的,Web应用程序中给定路径的实际路径
public static String getRealPath(ServletContext servletContext, String path) throws FileNotFoundException { Assert.notNull(servletContext, "ServletContext must not be null"); // Interpret location as relative to the web application root directory. if (!path.startsWith("/")) { path = "/" + path; } String realPath = servletContext.getRealPath(path); if (realPath == null) { throw new FileNotFoundException( "ServletContext resource [" + path + "] cannot be resolved to absolute file path - " + "web application archive not expanded?"); } return realPath; }
6.通过一个请求确定会话Session的标识(Id)
public static String getSessionId(HttpServletRequest request) { Assert.notNull(request, "Request must not be null"); HttpSession session = request.getSession(false); return (session != null ? session.getId() : null); }
7.通过一个请求,通过name获取session中的属性,如果session中没有属性或者没有session会返回一个null
public static Object getSessionAttribute(HttpServletRequest request, String name) { Assert.notNull(request, "Request must not be null"); HttpSession session = request.getSession(false); return (session != null ? session.getAttribute(name) : null); }
8.和7中方法相似,只不过不会返回null,会抛出异常
public static Object getRequiredSessionAttribute(HttpServletRequest request, String name) throws IllegalStateException { Object attr = getSessionAttribute(request, name); if (attr == null) { throw new IllegalStateException("No session attribute '" + name + "' found"); } return attr; }
9.通过给定的名称和值设置session中的属性,若果session值为空,会移除session的属性
public static void setSessionAttribute(HttpServletRequest request, String name, Object value) { Assert.notNull(request, "Request must not be null"); if (value != null) { request.getSession().setAttribute(name, value); } else { HttpSession session = request.getSession(false); if (session != null) { session.removeAttribute(name); } } }
10.获取指定的会话属性,如果没有找到的话会创建并设置新的属性。其中给定的类需要有一个公共的无参构造函数。
public static Object getOrCreateSessionAttribute(HttpSession session, String name, Class<?> clazz) throws IllegalArgumentException { Assert.notNull(session, "Session must not be null"); Object sessionObject = session.getAttribute(name); if (sessionObject == null) { try { sessionObject = clazz.newInstance(); } catch (InstantiationException ex) { throw new IllegalArgumentException( "Could not instantiate class [" + clazz.getName() + "] for session attribute '" + name + "': " + ex.getMessage()); } catch (IllegalAccessException ex) { throw new IllegalArgumentException( "Could not access default constructor of class [" + clazz.getName() + "] for session attribute '" + name + "': " + ex.getMessage()); } session.setAttribute(name, sessionObject); } return sessionObject; }
11.返回给定会话的最佳可用互斥量:即为给定会话同步的对象。返回会话互斥属性(如果可用);通常情况下,这意味着需要定义HttpSessionMutexListener在{code web.xml}中。 回到HttpSession本身如果没有找到互斥属性。会话互斥确保在期间是相同的对象会话的整个生命周期,在定义的密钥下可用由SESSION_MUTEX_ATTRIBUTE常量定义。 它作为一个在当前会话上同步锁定的安全引用。在很多情况下,HttpSession引用本身是一个安全的互斥体同样,因为它始终是相同的对象参考相同的活动逻辑会话。 但是,这并不能保证不同的servlet容器; 唯一的100%安全方式是会话互斥。
public static Object getSessionMutex(HttpSession session) { Assert.notNull(session, "Session must not be null"); Object mutex = session.getAttribute(SESSION_MUTEX_ATTRIBUTE); if (mutex == null) { mutex = session; } return mutex; }
12.返回指定类型的合适的请求对象,如果可用,会unwrapping给定的request请求
public static <T> T getNativeRequest(ServletRequest request, Class<T> requiredType) { if (requiredType != null) { if (requiredType.isInstance(request)) { return (T) request; } else if (request instanceof ServletRequestWrapper) { return getNativeRequest(((ServletRequestWrapper) request).getRequest(), requiredType); } } return null; }
13.和12方法类似,应用于response
public static <T> T getNativeResponse(ServletResponse response, Class<T> requiredType) { if (requiredType != null) { if (requiredType.isInstance(response)) { return (T) response; } else if (response instanceof ServletResponseWrapper) { return getNativeResponse(((ServletResponseWrapper) response).getResponse(), requiredType); } } return null; }
14.判断请求是否是一个包含(Include)请求,即不是从外部进入的顶级Http请求。
public static boolean isIncludeRequest(ServletRequest request) { return (request.getAttribute(INCLUDE_REQUEST_URI_ATTRIBUTE) != null); }
15.Servlet规范的错误属性,Servlet2.3规范中定义的属性,用于错误页面直接呈现,而不是通过Servlet容器的错误页面解析
public static void exposeErrorRequestAttributes(HttpServletRequest request, Throwable ex, String servletName) { exposeRequestAttributeIfNotPresent(request, ERROR_STATUS_CODE_ATTRIBUTE, HttpServletResponse.SC_OK); exposeRequestAttributeIfNotPresent(request, ERROR_EXCEPTION_TYPE_ATTRIBUTE, ex.getClass()); exposeRequestAttributeIfNotPresent(request, ERROR_MESSAGE_ATTRIBUTE, ex.getMessage()); exposeRequestAttributeIfNotPresent(request, ERROR_EXCEPTION_ATTRIBUTE, ex); exposeRequestAttributeIfNotPresent(request, ERROR_REQUEST_URI_ATTRIBUTE, request.getRequestURI()); exposeRequestAttributeIfNotPresent(request, ERROR_SERVLET_NAME_ATTRIBUTE, servletName); }
16.如果不存在? 公开指定的请求属性 (有疑问)
private static void exposeRequestAttributeIfNotPresent(ServletRequest request, String name, Object value) { if (request.getAttribute(name) == null) { request.setAttribute(name, value); } }
17.清除类似方法15中定义的错误属性,遵循servlet2.3规范
public static void clearErrorRequestAttributes(HttpServletRequest request) { request.removeAttribute(ERROR_STATUS_CODE_ATTRIBUTE); request.removeAttribute(ERROR_EXCEPTION_TYPE_ATTRIBUTE); request.removeAttribute(ERROR_MESSAGE_ATTRIBUTE); request.removeAttribute(ERROR_EXCEPTION_ATTRIBUTE); request.removeAttribute(ERROR_REQUEST_URI_ATTRIBUTE); request.removeAttribute(ERROR_SERVLET_NAME_ATTRIBUTE); }
18.将给定的Map公开为请求属性,使用键作为属性名称并将值作为相应的属性值。 键需要是字符串
public static void exposeRequestAttributes(ServletRequest request, Map<String, ?> attributes) { Assert.notNull(request, "Request must not be null"); Assert.notNull(attributes, "Attributes Map must not be null"); for (Map.Entry<String, ?> entry : attributes.entrySet()) { request.setAttribute(entry.getKey(), entry.getValue()); } }
19.检索具有给定名称的第一个cookie。注意多个Cookie可以具有相同的名称,但路径和域不同。
public static Cookie getCookie(HttpServletRequest request, String name) { Assert.notNull(request, "Request must not be null"); Cookie cookies[] = request.getCookies(); if (cookies != null) { for (Cookie cookie : cookies) { if (name.equals(cookie.getName())) { return cookie; } } } return null; }
20.检查请求中是否有通过按钮或者图像的特定的输入类型=“submit”参数
public static boolean hasSubmitParameter(ServletRequest request, String name) { Assert.notNull(request, "Request must not be null"); if (request.getParameter(name) != null) { return true; } for (String suffix : SUBMIT_IMAGE_SUFFIXES) { if (request.getParameter(name + suffix) != null) { return true; } } return false; }
21.从给定的请求参数中获取一个命名参数。可以参阅方法{findParameterValue}用于查找算法的描述
public static String findParameterValue(ServletRequest request, String name) { return findParameterValue(request.getParameterMap(), name); }
22.获取参数值通过以下的算法:(算法补充)
public static String findParameterValue(Map<String, ?> parameters, String name) { // First try to get it as a normal name=value parameter Object value = parameters.get(name); if (value instanceof String[]) { String[] values = (String[]) value; return (values.length > 0 ? values[0] : null); } else if (value != null) { return value.toString(); } // If no value yet, try to get it as a name_value=xyz parameter String prefix = name + "_"; for (String paramName : parameters.keySet()) { if (paramName.startsWith(prefix)) { // Support images buttons, which would submit parameters as name_value.x=123 for (String suffix : SUBMIT_IMAGE_SUFFIXES) { if (paramName.endsWith(suffix)) { return paramName.substring(prefix.length(), paramName.length() - suffix.length()); } } return paramName.substring(prefix.length()); } } // We couldn't find the parameter value... return null; }
23.返回包含具有给定前缀的所有参数的map。将单个值映射为String,多个值映射到String数组。
public static Map<String, Object> getParametersStartingWith(ServletRequest request, String prefix) { Assert.notNull(request, "Request must not be null"); Enumeration<String> paramNames = request.getParameterNames(); Map<String, Object> params = new TreeMap<String, Object>(); if (prefix == null) { prefix = ""; } while (paramNames != null && paramNames.hasMoreElements()) { String paramName = paramNames.nextElement(); if ("".equals(prefix) || paramName.startsWith(prefix)) { String unprefixed = paramName.substring(prefix.length()); String[] values = request.getParameterValues(paramName); if (values == null || values.length == 0) { // Do nothing, no values found at all. } else if (values.length > 1) { params.put(unprefixed, values); } else { params.put(unprefixed, values[0]); } } } return params; }
24.返回请求中指定的目标页面
public static int getTargetPage(ServletRequest request, String paramPrefix, int currentPage) { Enumeration<String> paramNames = request.getParameterNames(); while (paramNames.hasMoreElements()) { String paramName = paramNames.nextElement(); if (paramName.startsWith(paramPrefix)) { for (int i = 0; i < WebUtils.SUBMIT_IMAGE_SUFFIXES.length; i++) { String suffix = WebUtils.SUBMIT_IMAGE_SUFFIXES[i]; if (paramName.endsWith(suffix)) { paramName = paramName.substring(0, paramName.length() - suffix.length()); } } return Integer.parseInt(paramName.substring(paramPrefix.length())); } } return currentPage; }
25.从给定的请求URL路径中提取URL文件名
public static String extractFilenameFromUrlPath(String urlPath) { String filename = extractFullFilenameFromUrlPath(urlPath); int dotIndex = filename.lastIndexOf('.'); if (dotIndex != -1) { filename = filename.substring(0, dotIndex); } return filename; }
26.从给定的请求URL路径中提取完整的URL文件名(包括文件扩展名)。正确地解析嵌套路径,例如“/products/view.html”。
public static String extractFullFilenameFromUrlPath(String urlPath) { int end = urlPath.indexOf(';'); if (end == -1) { end = urlPath.indexOf('?'); if (end == -1) { end = urlPath.length(); } } int begin = urlPath.lastIndexOf('/', end) + 1; return urlPath.substring(begin, end); }27.用矩阵变量解析给定的字符串。 一个例子字符串会看起来像这样{@code“q1 = a; q1 = b; q2 = a,b,c”}。 结果地图将包含
使用值{@code [“a”,“b”]}的密钥{@code“q1”}和{@code“q2”}和{@code [“a”,“b”,“c”]}。
public static MultiValueMap<String, String> parseMatrixVariables(String matrixVariables) { MultiValueMap<String, String> result = new LinkedMultiValueMap<String, String>(); if (!StringUtils.hasText(matrixVariables)) { return result; } StringTokenizer pairs = new StringTokenizer(matrixVariables, ";"); while (pairs.hasMoreTokens()) { String pair = pairs.nextToken(); int index = pair.indexOf('='); if (index != -1) { String name = pair.substring(0, index); String rawValue = pair.substring(index + 1); for (String value : StringUtils.commaDelimitedListToStringArray(rawValue)) { result.add(name, value); } } else { result.add(pair, ""); } } return result; }