spring的WebUtils类源码解析

参考文章: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. Object attr = getSessionAttribute(request, name);
  13. if (attr == null) {
  14. throw new IllegalStateException("No session attribute ‘" + name + "‘ found");
  15. }
  16. return attr;
  17. }

(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. * Extract the full URL filename (including file extension) from the given request URL path.
  17. * Correctly resolves nested paths such as "/products/view.html" as well.
  18. * @param urlPath the request URL path (e.g. "/products/index.html")
  19. * @return the extracted URI filename (e.g. "index.html")
  20. */
  21. public static String extractFullFilenameFromUrlPath(String urlPath) {
  22. int end = urlPath.indexOf(‘;‘);
  23. if (end == -1) {
  24. end = urlPath.indexOf(‘?‘);
  25. if (end == -1) {
  26. end = urlPath.length();
  27. }
  28. }
  29. int begin = urlPath.lastIndexOf(‘/‘, end) + 1;
  30. return urlPath.substring(begin, end);
  31. }
时间: 2024-08-28 15:50:19

spring的WebUtils类源码解析的相关文章

java.lang.Void类源码解析_java - JAVA

文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 在一次源码查看ThreadGroup的时候,看到一段代码,为以下: /* * @throws NullPointerException if the parent argument is {@code null} * @throws SecurityException if the current thread cannot create a * thread in the specified thread grou

Java集合---Array类源码解析

Java集合---Array类源码解析              ---转自:牛奶.不加糖 一.Arrays.sort()数组排序 Java Arrays中提供了对所有类型的排序.其中主要分为Primitive(8种基本类型)和Object两大类. 基本类型:采用调优的快速排序: 对象类型:采用改进的归并排序. 1.对于基本类型源码分析如下(以int[]为例): Java对Primitive(int,float等原型数据)数组采用快速排序,对Object对象数组采用归并排序.对这一区别,sun在

【Spring】Spring IOC原理及源码解析之scope=request、session

一.容器 1. 容器 抛出一个议点:BeanFactory是IOC容器,而ApplicationContex则是Spring容器. 什么是容器?Collection和Container这两个单词都有存放什么东西的意思,但是放在程序猿的世界,却注定是千差万别.Collection,集合,存放obj instanceof Class为true的一类对象,重点在于存放:Container,容器,可以存放各种各样的obj,但不仅仅是存放,他被称为容器,更重要的是他能管理存放对象的生命周期和依赖. 容器:

Object类源码解析

本文的分析基于JDK 1.8 Java中所有的类都继承自Object类. Object类的源码解析 1.void registerNatives() private static native void registerNatives(); static { registerNatives(); } 1 2 3 4 5 1 2 3 4 5 该方法只是对几个本地方法进行注册(即初始化java方法映射到C的方法).需要注意的是,很多类中都有这个方法,但是执行注册的目标是不同的.System类中也有该

java-AbstractCollection类-源码解析

转载:原文地址 http://www.cnblogs.com/android-blogs/p/5566212.html 一.Collection接口 从<Java集合:整体结构>一文中我们知道所有的List和Set都继承自Collection接口,该接口类提供了集合最基本的方法,虽然List接口和Set等都有一些自己独有的方法,但是基本的操作类似.我们先看下Collection接口提供的方法: 总体上可以将Collection的方法分为以下几大类: 1.增加(add/addAll) 2.删除(

Scroller类源码解析及其应用(一)

滑动是我们在自定义控件时候经常遇见的难听,让新手们倍感困惑,这篇文章主要介绍Scroller类的源码,告诉打击这个到底有什么用,怎么使用它来控制滑动.另外,我还会结合一个简单的例子,来看一下这个类的应用. 要说明Scroller类,我们往往要从另外两个方法说起,一个是ScrollTo(),一个是ScrollBy() 这两个方法我们可以在View的源码看到,我们知道其实每个空间都有滚动条,只是有的我们将它隐藏,所以我们看不见 下面是ScrollTo方法 /** * Set the scrolled

Spring Boot自动配置源码解析(基于Spring Boot 2.0.2.RELEASE)

在Spring Boot官方介绍中,首一段话是这样的(如下图).我们可以大概了解到其所表达的含义:我们可以利用Spring Boot写很少的配置来创建一个非常方便的基于Spring整合第三方类库的单体企业级应用.相信使用过Spring Boot的人都知道,她在这方面从前到后的一系列整合.本篇文字将带你进入具体的实现细节. 首先我们写一段Spring Boot应用启动类的代码如下: 1 package com.springTest; 2 3 import org.springframework.b

Android中的ViewRootImpl类源码解析

转载请注明出处 http://blog.csdn.net/qianhaifeng2012/article/details/51737370 ViewRoot目前这个类已经没有了,是老版本中的一个类,在Android2.2以后用ViewRootImpl代替ViewRoot,对应于ViewRootImpl.java,他是链接WindowManager和DecorView的纽带,另外View的绘制也是通过ViewRootImpl来完成的. 它的主要作用我的总结为如下: A:链接WindowManage

Java集合---Arrays类源码解析

一.Arrays.sort()数组排序 Java Arrays中提供了对所有类型的排序.其中主要分为Primitive(8种基本类型)和Object两大类. 基本类型:采用调优的快速排序: 对象类型:采用改进的归并排序. 1.对于基本类型源码分析如下(以int[]为例): Java对Primitive(int,float等原型数据)数组采用快速排序,对Object对象数组采用归并排序.对这一区别,sun在<<The Java Tutorial>>中做出的解释如下: The sort