spring的WebUtils类源码解析

来源:互联网 发布:淘宝快递上门取件时间 编辑:程序博客网 时间:2024/05/21 06:11

参考文章:1. http://www.ibm.com/developerworks/cn/java/j-lo-spring-utils1/

               2.spring源码
WebUtils
位于 org.springframework.web.util 包中的 WebUtils 是一个非常好用的工具类,它对很多 Servlet API 提供了易用的代理方法,降低了访问 Servlet API 的复杂度,可以将其看成是常用 Servlet API 方法的门面类。
下面这些方法为访问 HttpServletRequest 和 HttpSession 中的对象和属性带来了方便:

 

(1)getSessionAttribute

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

Java代码  收藏代码
  1. /** 
  2.  * Check the given request for a session attribute of the given name. 
  3.  * Returns null if there is no session or if the session has no such attribute. 
  4.  * Does not create a new session if none has existed before! 
  5.  * @param request current HTTP request 
  6.  * @param name the name of the session attribute 
  7.  * @return the value of the session attribute, or <code>null</code> if not found 
  8.  */  
  9. public static Object getSessionAttribute(HttpServletRequest request, String name) {  
  10.     Assert.notNull(request, "Request must not be null");  
  11.     HttpSession session = request.getSession(false);  
  12.     return (session != null ? session.getAttribute(name) : null);  
  13. }  

 (2)getRequiredSessionAttribute

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

Java代码  收藏代码
  1. /** 
  2.  * Check the given request for a session attribute of the given name. 
  3.  * Throws an exception if there is no session or if the session has no such 
  4.  * attribute. Does not create a new session if none has existed before! 
  5.  * @param request current HTTP request 
  6.  * @param name the name of the session attribute 
  7.  * @return the value of the session attribute, or <code>null</code> if not found 
  8.  * @throws IllegalStateException if the session attribute could not be found 
  9.  */  
  10. public static Object getRequiredSessionAttribute(HttpServletRequest request, String name)  
  11.     throws IllegalStateException {  
  12.   
  13.     Object attr = getSessionAttribute(request, name);  
  14.     if (attr == null) {  
  15.         throw new IllegalStateException("No session attribute '" + name + "' found");  
  16.     }  
  17.     return attr;  
  18. }  

 (3)setSessionAttribute

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

Java代码  收藏代码
  1. /** 
  2.  * Set the session attribute with the given name to the given value. 
  3.  * Removes the session attribute if value is null, if a session existed at all. 
  4.  * Does not create a new session if not necessary! 
  5.  * @param request current HTTP request 
  6.  * @param name the name of the session attribute 
  7.  * @param value the value of the session attribute 
  8.  */  
  9. public static void setSessionAttribute(HttpServletRequest request, String name, Object value) {  
  10.     Assert.notNull(request, "Request must not be null");  
  11.     if (value != null) {  
  12.         request.getSession().setAttribute(name, value);  
  13.     }  
  14.     else {  
  15.         HttpSession session = request.getSession(false);  
  16.         if (session != null) {  
  17.             session.removeAttribute(name);  
  18.         }  
  19.     }  
  20. }  

 (4)exposeRequestAttributes

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

Java代码  收藏代码
  1. /** 
  2.  * Expose the given Map as request attributes, using the keys as attribute names 
  3.  * and the values as corresponding attribute values. Keys need to be Strings. 
  4.  * @param request current HTTP request 
  5.  * @param attributes the attributes Map 
  6.  */  
  7. public static void exposeRequestAttributes(ServletRequest request, Map<String, ?> attributes) {  
  8.     Assert.notNull(request, "Request must not be null");  
  9.     Assert.notNull(attributes, "Attributes Map must not be null");  
  10.     for (Map.Entry<String, ?> entry : attributes.entrySet()) {  
  11.         request.setAttribute(entry.getKey(), entry.getValue());  
  12.     }  
  13. }  

 (5)getCookie

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

Java代码  收藏代码
  1. /** 
  2.  * Retrieve the first cookie with the given name. Note that multiple 
  3.  * cookies can have the same name but different paths or domains. 
  4.  * @param request current servlet request 
  5.  * @param name cookie name 
  6.  * @return the first cookie with the given name, or <code>null</code> if none is found 
  7.  */  
  8. public static Cookie getCookie(HttpServletRequest request, String name) {  
  9.     Assert.notNull(request, "Request must not be null");  
  10.     Cookie cookies[] = request.getCookies();  
  11.     if (cookies != null) {  
  12.         for (Cookie cookie : cookies) {  
  13.             if (name.equals(cookie.getName())) {  
  14.                 return cookie;  
  15.             }  
  16.         }  
  17.     }  
  18.     return null;  
  19. }  

 (6)extractFilenameFromUrlPath

从Url地址中截取文件名称

Java代码  收藏代码
  1. /** 
  2.  * Extract the URL filename from the given request URL path. 
  3.  * Correctly resolves nested paths such as "/products/view.html" as well. 
  4.  * @param urlPath the request URL path (e.g. "/index.html") 
  5.  * @return the extracted URI filename (e.g. "index") 
  6.  */  
  7. public static String extractFilenameFromUrlPath(String urlPath) {  
  8.     String filename = extractFullFilenameFromUrlPath(urlPath);  
  9.     int dotIndex = filename.lastIndexOf('.');  
  10.     if (dotIndex != -1) {  
  11.         filename = filename.substring(0, dotIndex);  
  12.     }  
  13.     return filename;  
  14. }  
  15.   
  16. /** 
  17.  * Extract the full URL filename (including file extension) from the given request URL path. 
  18.  * Correctly resolves nested paths such as "/products/view.html" as well. 
  19.  * @param urlPath the request URL path (e.g. "/products/index.html") 
  20.  * @return the extracted URI filename (e.g. "index.html") 
  21.  */  
  22. public static String extractFullFilenameFromUrlPath(String urlPath) {  
  23.     int end = urlPath.indexOf(';');  
  24.     if (end == -1) {  
  25.         end = urlPath.indexOf('?');  
  26.         if (end == -1) {  
  27.             end = urlPath.length();  
  28.         }  
  29.     }  
  30.     int begin = urlPath.lastIndexOf('/', end) + 1;  
  31.     return urlPath.substring(begin, end);  

0 0
原创粉丝点击