搜课云网 > 郑州北大青鸟 > 资讯总汇 > spring的WebUtils类源码解析

spring的WebUtils类源码解析

机构:郑州北大青鸟 时间:2015-07-02 11:14:39 点击:682

  WebUtils

  位于 org.springframework.web.util 包中的 WebUtils 是一个非常好用的工具类,它对很多 Servlet API 提供了易用的代理方法,降低了访问 Servlet API 的复杂度,可以将其看成是常用 Servlet API 方法的门面类。

  下面这些方法为访问 HttpServletRequest 和 HttpSession 中的对象和属性带来了方便:

  (1)getSessionAttribute

  获取 HttpSession 特定属性名的对象,否则您必须通过 request.getSession.getAttribute(name) 完成相同的操作;

  Java代码

  /** * Check the given request for a session attribute of the given name. * Returns null if there is no session or if the session has no such attribute. * Does not create a new session if none has existed before! * @param request current HTTP request * @param name the name of the session attribute * @return the value of the session attribute, ornullif not found */ 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); }

  (2)getRequiredSessionAttribute

  和上一个方法类似,只不过强制要求 HttpSession 中拥有指定的属性,否则抛出异常;

  Java代码

  /** * Check the given request for a session attribute of the given name. * Throws an exception if there is no session or if the session has no such * attribute. Does not create a new session if none has existed before! * @param request current HTTP request * @param name the name of the session attribute * @return the value of the session attribute, ornullif not found * @throws IllegalStateException if the session attribute could not be found */ 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; }

  (3)setSessionAttribute

  给session中的指定属性设置值,若传入值为空,则从session中移除该属性

  Java代码

  /** * Set the session attribute with the given name to the given value. * Removes the session attribute if value is null, if a session existed at all. * Does not create a new session if not necessary! * @param request current HTTP request * @param name the name of the session attribute * @param value the value of the session attribute */ 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); } } }

  (4)exposeRequestAttributes

  将 Map 元素添加到 ServletRequest 的属性列表中,当请求被导向(forward)到下一个处理程序时,这些请求属性就可以被访问到了;

  Java代码

  /** * Expose the given Map as request attributes, using the keys as attribute names * and the values as corresponding attribute values. Keys need to be Strings. * @param request current HTTP request * @param attributes the attributes Map */ public static void exposeRequestAttributes(ServletRequest request, Mapattributes) { Assert.notNull(request, Request must not be null); Assert.notNull(attributes, Attributes Map must not be null); for (Map.Entryentry : attributes.entrySet()) { request.setAttribute(entry.getKey(), entry.getValue()); } }

  (5)getCookie

  获取 HttpServletRequest 中特定名字的 Cookie 对象。如果您需要创建 Cookie, Spring 也提供了一个方便的 CookieGenerator 工具类;

  Java代码

  /** * Retrieve the first cookie with the given name. Note that multiple * cookies can have the same name but different paths or domains. * @param request current servlet request * @param name cookie name * @return the first cookie with the given name, ornullif none is found */ 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; }

  (6)extractFilenameFromUrlPath

  从Url地址中截取文件名称

  Java代码

  /** * Extract the URL filename from the given request URL path. * Correctly resolves nested paths such as /products/view.html as well. * @param urlPath the request URL path (e.g. /index.html) * @return the extracted URI filename (e.g. index) */ public static String extractFilenameFromUrlPath(String urlPath) { String filename = extractFullFilenameFromUrlPath(urlPath); int dotIndex = filename.lastIndexOf('.'); if (dotIndex != -1) { filename = filename.substring(0, dotIndex); } return filename; } /** * Extract the full URL filename (including file extension) from the given request URL path. * Correctly resolves nested paths such as /products/view.html as well. * @param urlPath the request URL path (e.g. /products/index.html) * @return the extracted URI filename (e.g. index.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); }