标签:java
参考文章: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) 完成相同的操作;
-
-
-
-
-
-
-
-
- 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 中拥有指定的属性,否则抛出异常;
-
-
-
-
-
-
-
-
-
- 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中移除该属性
-
-
-
-
-
-
-
-
- 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)到下一个处理程序时,这些请求属性就可以被访问到了;
-
-
-
-
-
-
- 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());
- }
- }
(5)getCookie
获取 HttpServletRequest 中特定名字的 Cookie 对象。如果您需要创建 Cookie, Spring 也提供了一个方便的 CookieGenerator 工具类;
-
-
-
-
-
-
-
- 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地址中截取文件名称
-
-
-
-
-
-
- public static String extractFilenameFromUrlPath(String urlPath) {
- String filename = extractFullFilenameFromUrlPath(urlPath);
- int dotIndex = filename.lastIndexOf(‘.‘);
- if (dotIndex != -1) {
- filename = filename.substring(0, dotIndex);
- }
- return filename;
- }
-
-
-
-
-
-
-
- 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);
- }
spring的WebUtils类源码解析
标签:java
原文地址:http://blog.csdn.net/u010317990/article/details/46309531