spring的WebUtils工具类部分方法解析
来源:互联网 发布:webpack配置config.js 编辑:程序博客网 时间:2024/06/06 02:18
如果你的项目中使用到了Spring(当然大点的项目都用到了),对session的操作就方便多了。如果需要在Session中取值,可以用WebUtils工具(org.springframework.web.util.WebUtils)的getSessionAttribute(HttpServletRequestrequest, String name)方法
(1)getSessionAttribute
获取 HttpSession 特定属性名的对象,否则您必须通过 request.getSession.getAttribute(name) 完成相同的操作;
- /**
- * 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, or <code>null</code> if 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 中拥有指定的属性,否则抛出异常;
- /**
- * 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, or <code>null</code> if 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中移除该属性
- /**
- * 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)到下一个处理程序时,这些请求属性就可以被访问到了;
- /**
- * 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, 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 工具类;
- /**
- * 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, or <code>null</code> if 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地址中截取文件名称
- /**
- * 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);
- }
0 0
- spring的WebUtils工具类部分方法解析
- spring的WebUtils类源码解析
- Spring中的WebUtils类
- Spring中的WebUtils
- Sping 中包的方法WebUtils.getParametersStartingWith(request,String);
- WebUtils
- Spring 的优秀工具类盘点,第 2 部分: 特殊字符转义和方法入参检测工具类
- Spring 的优秀工具类盘点,第 2 部分: 特殊字符转义和方法入参检测工具类
- Spring 的优秀工具类盘点,第 2 部分: 特殊字符转义和方法入参检测工具类
- Spring 的优秀工具类盘点,第 2 部分: 特殊字符转义和方法入参检测工具类
- Spring 的优秀工具类盘点,第 2 部分: 特殊字符转义和方法入参检测工具类
- Spring 的优秀工具类盘点,第 2 部分: 特殊字符转义和方法入参检测工具类
- Spring 的优秀工具类盘点,第 2 部分: 特殊字符转义和方法入参检测工具类
- Spring 的优秀工具类盘点,第 2 部分: 特殊字符转义和方法入参检测工具类
- Spring 的优秀工具类盘点,第 2 部分: 特殊字符转义和方法入参检测工具类
- Spring 的优秀工具类盘点,第 2 部分: 特殊字符转义和方法入参检测工具类
- Spring 的优秀工具类盘点,第 2 部分: 特殊字符转义和方法入参检测工具类
- Spring 的优秀工具类盘点,第 2 部分: 特殊字符转义和方法入参检测工具类
- Percent百分比布局--Android新布局
- Win7,64位下Python 读取csv文件(Excel转化的)并绘制图表
- angularJS界面跳转,及传参
- 三维操作打游戏
- url中,中文乱码的问题
- spring的WebUtils工具类部分方法解析
- 安卓基础DAY06 笔记
- mac os(ubuntu等一样)下启动tomcat,输出控制台信息
- 使用GitHub打造你的个人主页
- FIFO例程
- 深入理解 Java try-with-resource 语法糖
- 算法导论 15-3 编辑距离
- Matlab与线性代数--矩阵的正交分解
- 使用Android-PickerView修改item的字体大小(坑)