`
234390216
  • 浏览: 10232224 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
博客专栏
A5ee55b9-a463-3d09-9c78-0c0cf33198cd
Oracle基础
浏览量:462595
Ad26f909-6440-35a9-b4e9-9aea825bd38e
springMVC介绍
浏览量:1775423
Ce363057-ae4d-3ee1-bb46-e7b51a722a4b
Mybatis简介
浏览量:1398309
Bdeb91ad-cf8a-3fe9-942a-3710073b4000
Spring整合JMS
浏览量:394994
5cbbde67-7cd5-313c-95c2-4185389601e7
Ehcache简介
浏览量:679961
Cc1c0708-ccc2-3d20-ba47-d40e04440682
Cas简介
浏览量:530846
51592fc3-854c-34f4-9eff-cb82d993ab3a
Spring Securi...
浏览量:1183872
23e1c30e-ef8c-3702-aa3c-e83277ffca91
Spring基础知识
浏览量:467771
4af1c81c-eb9d-365f-b759-07685a32156e
Spring Aop介绍
浏览量:151370
2f926891-9e7a-3ce2-a074-3acb2aaf2584
JAXB简介
浏览量:68128
社区版块
存档分类
最新评论

SpringMVC之RequestContextUtils工具类

阅读更多

RequestContextUtils类

RequestContextUtils类是Spring提供的用于从HttpServletRequest上下文中获取特殊对象的工具类。该工具类虽然是属于Spring的一部分,但是如果在应用中我们有需要直接获取相关信息的需求,我们也可以直接使用。

从request中获取WebApplicationContext。

RequestContextUtils.getWebApplicationContext(request);

从request中获取LocaleResolver或Locale。

RequestContextUtils.getLocaleResolver(request);
RequestContextUtils.getLocale(request);

从request中获取ThemeResolver或Theme。

RequestContextUtils.getThemeResolver(request);
RequestContextUtils.getTheme(request);

除了上面那些,我们还可以获取FlashMapManager和TimeZone等其它信息。以下是该工具类的完整代码。

public abstract class RequestContextUtils {

	/**
	 * Look for the WebApplicationContext associated with the DispatcherServlet
	 * that has initiated request processing.
	 * @param request current HTTP request
	 * @return the request-specific web application context
	 * @throws IllegalStateException if no servlet-specific context has been found
	 */
	public static WebApplicationContext getWebApplicationContext(ServletRequest request)
		throws IllegalStateException {

		return getWebApplicationContext(request, null);
	}

	/**
	 * Look for the WebApplicationContext associated with the DispatcherServlet
	 * that has initiated request processing, and for the global context if none
	 * was found associated with the current request. This method is useful to
	 * allow components outside the framework, such as JSP tag handlers,
	 * to access the most specific application context available.
	 * @param request current HTTP request
	 * @param servletContext current servlet context
	 * @return the request-specific WebApplicationContext, or the global one
	 * if no request-specific context has been found
	 * @throws IllegalStateException if neither a servlet-specific nor a
	 * global context has been found
	 */
	public static WebApplicationContext getWebApplicationContext(
			ServletRequest request, ServletContext servletContext) throws IllegalStateException {

		WebApplicationContext webApplicationContext = (WebApplicationContext) request.getAttribute(
				DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
		if (webApplicationContext == null) {
			if (servletContext == null) {
				throw new IllegalStateException("No WebApplicationContext found: not in a DispatcherServlet request?");
			}
			webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
		}
		return webApplicationContext;
	}

	/**
	 * Return the LocaleResolver that has been bound to the request by the
	 * DispatcherServlet.
	 * @param request current HTTP request
	 * @return the current LocaleResolver, or {@code null} if not found
	 */
	public static LocaleResolver getLocaleResolver(HttpServletRequest request) {
		return (LocaleResolver) request.getAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE);
	}

	/**
	 * Retrieve the current locale from the given request, using the
	 * LocaleResolver bound to the request by the DispatcherServlet
	 * (if available), falling back to the request's accept-header Locale.
	 * <p>This method serves as a straightforward alternative to the standard
	 * Servlet {@link javax.servlet.http.HttpServletRequest#getLocale()} method,
	 * falling back to the latter if no more specific locale has been found.
	 * <p>Consider using {@link org.springframework.context.i18n.LocaleContextHolder#getLocale()}
	 * which will normally be populated with the same Locale.
	 * @param request current HTTP request
	 * @return the current locale for the given request, either from the
	 * LocaleResolver or from the plain request itself
	 * @see #getLocaleResolver
	 * @see org.springframework.context.i18n.LocaleContextHolder#getLocale()
	 */
	public static Locale getLocale(HttpServletRequest request) {
		LocaleResolver localeResolver = getLocaleResolver(request);
		return (localeResolver != null ? localeResolver.resolveLocale(request) : request.getLocale());
	}

	/**
	 * Retrieve the current time zone from the given request, using the
	 * TimeZoneAwareLocaleResolver bound to the request by the DispatcherServlet
	 * (if available), falling back to the system's default time zone.
	 * <p>Note: This method returns {@code null} if no specific time zone can be
	 * resolved for the given request. This is in contrast to {@link #getLocale}
	 * where there is always the request's accept-header locale to fall back to.
	 * <p>Consider using {@link org.springframework.context.i18n.LocaleContextHolder#getTimeZone()}
	 * which will normally be populated with the same TimeZone: That method only
	 * differs in terms of its fallback to the system time zone if the LocaleResolver
	 * hasn't provided provided a specific time zone (instead of this method's {@code null}).
	 * @param request current HTTP request
	 * @return the current time zone for the given request, either from the
	 * TimeZoneAwareLocaleResolver or {@code null} if none associated
	 * @see #getLocaleResolver
	 * @see org.springframework.context.i18n.LocaleContextHolder#getTimeZone()
	 */
	public static TimeZone getTimeZone(HttpServletRequest request) {
		LocaleResolver localeResolver = getLocaleResolver(request);
		if (localeResolver instanceof LocaleContextResolver) {
			LocaleContext localeContext = ((LocaleContextResolver) localeResolver).resolveLocaleContext(request);
			if (localeContext instanceof TimeZoneAwareLocaleContext) {
				return ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
			}
		}
		return null;
	}

	/**
	 * Return the ThemeResolver that has been bound to the request by the
	 * DispatcherServlet.
	 * @param request current HTTP request
	 * @return the current ThemeResolver, or {@code null} if not found
	 */
	public static ThemeResolver getThemeResolver(HttpServletRequest request) {
		return (ThemeResolver) request.getAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE);
	}

	/**
	 * Return the ThemeSource that has been bound to the request by the
	 * DispatcherServlet.
	 * @param request current HTTP request
	 * @return the current ThemeSource
	 */
	public static ThemeSource getThemeSource(HttpServletRequest request) {
		return (ThemeSource) request.getAttribute(DispatcherServlet.THEME_SOURCE_ATTRIBUTE);
	}

	/**
	 * Retrieves the current theme from the given request, using the ThemeResolver
	 * and ThemeSource bound to the request by the DispatcherServlet.
	 * @param request current HTTP request
	 * @return the current theme, or {@code null} if not found
	 * @see #getThemeResolver
	 */
	public static Theme getTheme(HttpServletRequest request) {
		ThemeResolver themeResolver = getThemeResolver(request);
		ThemeSource themeSource = getThemeSource(request);
		if (themeResolver != null && themeSource != null) {
			String themeName = themeResolver.resolveThemeName(request);
			return themeSource.getTheme(themeName);
		}
		else {
			return null;
		}
	}

	/**
	 * Return a read-only {@link Map} with "input" flash attributes saved on a
	 * previous request.
	 * @param request the current request
	 * @return a read-only Map, or {@code null} if not found
	 * @see FlashMap
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, ?> getInputFlashMap(HttpServletRequest request) {
		return (Map<String, ?>) request.getAttribute(DispatcherServlet.INPUT_FLASH_MAP_ATTRIBUTE);
	}

	/**
	 * Return the "output" FlashMap with attributes to save for a subsequent request.
	 * @param request the current request
	 * @return a {@link FlashMap} instance (never {@code null} within a DispatcherServlet request)
	 * @see FlashMap
	 */
	public static FlashMap getOutputFlashMap(HttpServletRequest request) {
		return (FlashMap) request.getAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE);
	}

	/**
	 * Return the FlashMapManager instance to save flash attributes with
	 * before a redirect.
	 * @param request the current request
	 * @return a {@link FlashMapManager} instance (never {@code null} within a DispatcherServlet request)
	 */
	public static FlashMapManager getFlashMapManager(HttpServletRequest request) {
		return (FlashMapManager) request.getAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE);
	}

}

(本文是基于Spring4.1.0所写)

0
0
分享到:
评论

相关推荐

    SpringMVC可以用的工具类

    在SpringMVC中,工具类(Util Classes)是开发者自定义的辅助类,用于提高代码的可重用性和可维护性。以下是一些SpringMVC中常见的工具类及其作用: 1. **日期时间工具类**:在处理日期和时间时,开发者通常会创建...

    SpringMVC文件上传和下载的工具类(完整项目)

    本文主要目的是记录自己基于SpringMVC实现的文件上传和下载的工具类的编写,代码经过测试可以直接运行在以后的项目中。开发的主要思路是对上传和下载文件进行抽象,把上传和下载的核心功能抽取出来分装成类。

    springMVC代码生成工具

    SpringMVC代码生成工具是一种基于Eclipse开发的插件,专为简化SpringMVC框架的开发流程而设计。它能够自动生成与MySQL数据库相关的模型、控制器、服务和DAO层的代码,大大提高了开发效率,降低了程序员手动编写重复...

    springmvc 实体层自动生成工具

    实体层自动生成工具则是开发者用来提高效率的重要辅助工具,它可以自动化地将数据库中的表结构映射为Java对象,减少了手动编写实体类的工作量。 SpringMVC实体层自动生成工具的主要功能包括: 1. 数据库表反向工程...

    使用注解SpringMVC从页面导出Excel和word文档的使用的jar包——使用ExportExcel工具类.rar

    总的来说,通过注解SpringMVC和ExportExcel工具类,我们可以轻松地实现在网页上导出Excel和Word文档的功能。这个过程涉及到SpringMVC的请求处理、响应设置,以及第三方库(如Apache POI)的使用,这些知识对于任何...

    java SpringMVC动态数据源的DataSource工具类

    本篇将详细讲解如何在SpringMVC中实现动态数据源的DataSource工具类。 首先,我们来看`MultiDataSource.java`这个文件。它通常会定义一个`DataSource`的工厂类,用于根据不同的需求动态地返回不同的数据源。在...

    基于mysql和oracle的springMvc代码生成工具

    在这个案例中,工具能根据MySQL或Oracle数据库中的表定义,自动生成对应的实体类、DAO(数据访问对象)、Service以及Controller层的代码。 3. **MySQL数据库**:MySQL是一款开源的关系型数据库管理系统,广泛应用于...

    java springMVC代码生成工具

    自动生成单表的html,js,controller,model,service,impl,dao,mapper. 其中提供单表的增删改查页面后端跳转、列表查询、增加、编辑、详情查询sql和方法。生成后壳根据自己需要增减。减少开发人员基础的代码开发,有...

    SpringMVC PPT_springmvc_

    SpringMVC 提供了 MockMVC 工具,可以方便地对 Controller 进行单元测试,无需部署到服务器。 总结,SpringMVC 以其强大的功能和灵活性,成为了 Java Web 开发的主流框架之一。通过理解并熟练掌握上述知识点,...

    SpringMVC之用户管理的源代码

    在这个“SpringMVC之用户管理的源代码”案例中,我们将深入探讨如何使用SpringMVC来实现一个基础的用户管理系统。 首先,我们需要了解SpringMVC的基本组件: 1. **DispatcherServlet**:它是SpringMVC的前端控制器...

    Spring+SpringMvc+Mybatis框架代码生成工具

    1. 使用方法 只需要注意这两个文件 配置文件说明: 配置好后 运行主程序 生成的文件已经分好路径 可直接粘贴到项目里,记得mybatis-config.xml.和 spring-service.xml里 注册下

    一个可以直接运行的基于SpringMVC的web框架1.1.12

    Excel工具类 Word工具类 Java NIO实现socket工具类 分布式session jdk升级到1.7 嵌入式redis服务(只支持linux) 1.0.13 修改默认的beanName生成策略,controller参数扩展 1.0.14 分布式session使用zookeeper 1.0.15 ...

    SpringMvc单元测试Junit

    在这个Demo中,我们将深入探讨SpringMvc如何与JUnit结合进行有效的单元测试,以及如何利用这些工具提升代码质量和可维护性。 首先,SpringMvc是Spring框架的一部分,专门用于构建Web应用程序的MVC(模型-视图-控制...

    jetbrick-springmvc jar包(包含源码)

    9. **工具类**:提供了一系列实用的工具类,如字符串操作、日期时间处理、JSON转换等,方便开发者进行日常开发。 10. **源码分析**:由于此jar包包含了源码,开发者可以通过阅读源码学习Jetbrick-SpringMVC的设计...

    SpringMVC4.0完整工具包资源(Eclipse和MyEclipse都可用)

    这个完整的工具包资源包含了所有你需要在Eclipse或MyEclipse环境中开发基于SpringMVC4.0应用的必要组件。 首先,让我们详细了解一下SpringMVC的核心概念: 1. **DispatcherServlet**:它是Spring MVC的前端控制器...

    SpringMVC 需要的jar

    - `spring-core`: 包含Spring的基础工具类和资源处理,如`ClassUtils`和`Resource`接口。 - `spring-beans`: 处理Bean的定义和实例化,实现依赖注入。 - `spring-aop`: 实现面向切面编程,提供声明式事务管理和...

    springMVC练手代码

    在SpringMVC中,控制器通常由带有`@Controller`注解的Java类实现,方法上使用`@RequestMapping`注解来映射URL。 5. **视图(View)**:视图负责展示数据。SpringMVC支持多种视图技术,如JSP、FreeMarker、Thymeleaf等...

    springmvc与redis4.5集群集成

    项目提供的`springmvc_redis`压缩包中,包含了完整的集成示例,包括SpringMVC配置、Redis工具类以及测试用例。你可以下载后在Eclipse中直接运行,体验集成效果。 通过上述两种方式,你可以根据实际需求灵活选择集成...

    springmvc postman工具,

    标题中的“SpringMVC”和“Postman工具”是指在Java Web开发中,利用Postman这一强大的API测试工具来辅助SpringMVC框架的应用程序进行接口测试。Postman是一款功能丰富的HTTP客户端,它允许开发者发送HTTP请求,从而...

Global site tag (gtag.js) - Google Analytics