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所写)
相关推荐
SpringMVC可以用的工具类,对初学者有点帮助,资源应该还不全面我也是刚接触后面了解后再补充
本文主要目的是记录自己基于SpringMVC实现的文件上传和下载的工具类的编写,代码经过测试可以直接运行在以后的项目中。开发的主要思路是对上传和下载文件进行抽象,把上传和下载的核心功能抽取出来分装成类。
java SpringMVC动态数据源的DataSource工具类,springmvc中配置数据源为工具类中的数据源,多线程使用不同数据源配置
SpringMVC4.0完整工具类数据包,Eclipse和MyEclipse环境下都可以使用。
elipse下的仅仅支持mysql数据库 的 springMvc 代码生成工具
Springmvc+poi 实现导入导出excel的controller方法和工具类
多个进程或线程同时(或着说在同一段时间内)访问同一资源会产生并发问题。
使用注解SpringMVC从页面导出Excel和word文档的使用的jar包——使用ExportExcel工具类
SpringMVC 常用注解类,视频讲解,带课程文档。SpringMVC 常用注解类
执行自动将数据库的表转化了类
1. 使用方法 只需要注意这两个文件 配置文件说明: 配置好后 运行主程序 生成的文件已经分好路径 可直接粘贴到项目里,记得mybatis-config.xml.和 spring-service.xml里 注册下
postman springmvc 辅助工具
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的rest风格的增删改查,适合springmvc的新手
支持mysql和oracle数据库反向生成springMvc各个层的代码的工具。
springmvc是常用的一种框架,学习springmvc非常重要,故需要良好教程资料
18、SpringMvc 中有个类把视图和数据都合并的一起的,叫什么? 19、怎么样把 ModelMap 里面的数据放入 Session 里面? 20、SpringMvc 怎么和 AJAX 相互调用的? 21、当一个方法向 AJAX 返回特殊对象,譬如 Object,List...
一份简单的SpringMvc的学习笔记,能简单和清楚的了解SpringMvc
SpringMVC文件上传Demo代码
这是一个使用SpringMVC实现用户管理的简单案例,增加初学者的认识和理解