下面列出了怎么用org.springframework.web.servlet.LocaleContextResolver的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Change the current locale to the specified locale and time zone context,
* storing the new locale context through the configured {@link LocaleResolver}.
* @param locale the new locale
* @param timeZone the new time zone
* @see LocaleContextResolver#setLocaleContext
* @see org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext
*/
public void changeLocale(Locale locale, TimeZone timeZone) {
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(this.request);
if (!(localeResolver instanceof LocaleContextResolver)) {
throw new IllegalStateException("Cannot change locale context if no LocaleContextResolver configured");
}
((LocaleContextResolver) localeResolver).setLocaleContext(this.request, this.response,
new SimpleTimeZoneAwareLocaleContext(locale, timeZone));
this.locale = locale;
this.timeZone = timeZone;
}
/**
* Change the current locale to the specified locale and time zone context,
* storing the new locale context through the configured {@link LocaleResolver}.
* @param locale the new locale
* @param timeZone the new time zone
* @see LocaleContextResolver#setLocaleContext
* @see org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext
*/
public void changeLocale(Locale locale, TimeZone timeZone) {
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(this.request);
if (!(localeResolver instanceof LocaleContextResolver)) {
throw new IllegalStateException("Cannot change locale context if no LocaleContextResolver configured");
}
((LocaleContextResolver) localeResolver).setLocaleContext(this.request, this.response,
new SimpleTimeZoneAwareLocaleContext(locale, timeZone));
this.locale = locale;
this.timeZone = timeZone;
}
/**
* Change the current locale to the specified locale and time zone context,
* storing the new locale context through the configured {@link LocaleResolver}.
* @param locale the new locale
* @param timeZone the new time zone
* @see LocaleContextResolver#setLocaleContext
* @see org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext
*/
public void changeLocale(Locale locale, TimeZone timeZone) {
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(this.request);
if (!(localeResolver instanceof LocaleContextResolver)) {
throw new IllegalStateException("Cannot change locale context if no LocaleContextResolver configured");
}
((LocaleContextResolver) localeResolver).setLocaleContext(this.request, this.response,
new SimpleTimeZoneAwareLocaleContext(locale, timeZone));
this.locale = locale;
this.timeZone = timeZone;
}
/**
* Change the current locale to the specified locale and time zone context,
* storing the new locale context through the configured {@link LocaleResolver}.
* @param locale the new locale
* @param timeZone the new time zone
* @see LocaleContextResolver#setLocaleContext
* @see org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext
*/
public void changeLocale(Locale locale, TimeZone timeZone) {
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(this.request);
if (!(localeResolver instanceof LocaleContextResolver)) {
throw new IllegalStateException("Cannot change locale context if no LocaleContextResolver configured");
}
((LocaleContextResolver) localeResolver).setLocaleContext(this.request, this.response,
new SimpleTimeZoneAwareLocaleContext(locale, timeZone));
this.locale = locale;
this.timeZone = timeZone;
}
/**
* Create a new RequestContext for the given request, using the given model attributes for Errors retrieval.
* <p>This works with all View implementations. It will typically be used by View implementations.
* <p>If a ServletContext is specified, the RequestContext will also work with a root
* WebApplicationContext (outside a DispatcherServlet).
* @param request current HTTP request
* @param response current HTTP response
* @param servletContext the servlet context of the web application (can be {@code null}; necessary for
* fallback to root WebApplicationContext)
* @param model the model attributes for the current view (can be {@code null}, using the request attributes
* for Errors retrieval)
* @see org.springframework.web.context.WebApplicationContext
* @see org.springframework.web.servlet.DispatcherServlet
*/
public RequestContext(HttpServletRequest request, @Nullable HttpServletResponse response,
@Nullable ServletContext servletContext, @Nullable Map<String, Object> model) {
this.request = request;
this.response = response;
this.model = model;
// Fetch WebApplicationContext, either from DispatcherServlet or the root context.
// ServletContext needs to be specified to be able to fall back to the root context!
WebApplicationContext wac = (WebApplicationContext) request.getAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE);
if (wac == null) {
wac = RequestContextUtils.findWebApplicationContext(request, servletContext);
if (wac == null) {
throw new IllegalStateException("No WebApplicationContext found: not in a DispatcherServlet " +
"request and no ContextLoaderListener registered?");
}
}
this.webApplicationContext = wac;
Locale locale = null;
TimeZone timeZone = null;
// Determine locale to use for this RequestContext.
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
if (localeResolver instanceof LocaleContextResolver) {
LocaleContext localeContext = ((LocaleContextResolver) localeResolver).resolveLocaleContext(request);
locale = localeContext.getLocale();
if (localeContext instanceof TimeZoneAwareLocaleContext) {
timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
}
}
else if (localeResolver != null) {
// Try LocaleResolver (we're within a DispatcherServlet request).
locale = localeResolver.resolveLocale(request);
}
this.locale = locale;
this.timeZone = timeZone;
// Determine default HTML escape setting from the "defaultHtmlEscape"
// context-param in web.xml, if any.
this.defaultHtmlEscape = WebUtils.getDefaultHtmlEscape(this.webApplicationContext.getServletContext());
// Determine response-encoded HTML escape setting from the "responseEncodedHtmlEscape"
// context-param in web.xml, if any.
this.responseEncodedHtmlEscape =
WebUtils.getResponseEncodedHtmlEscape(this.webApplicationContext.getServletContext());
this.urlPathHelper = new UrlPathHelper();
if (this.webApplicationContext.containsBean(RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME)) {
this.requestDataValueProcessor = this.webApplicationContext.getBean(
RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME, RequestDataValueProcessor.class);
}
}
/**
* Create a new RequestContext for the given request, using the given model attributes for Errors retrieval.
* <p>This works with all View implementations. It will typically be used by View implementations.
* <p>If a ServletContext is specified, the RequestContext will also work with a root
* WebApplicationContext (outside a DispatcherServlet).
* @param request current HTTP request
* @param response current HTTP response
* @param servletContext the servlet context of the web application (can be {@code null}; necessary for
* fallback to root WebApplicationContext)
* @param model the model attributes for the current view (can be {@code null}, using the request attributes
* for Errors retrieval)
* @see org.springframework.web.context.WebApplicationContext
* @see org.springframework.web.servlet.DispatcherServlet
*/
public RequestContext(HttpServletRequest request, @Nullable HttpServletResponse response,
@Nullable ServletContext servletContext, @Nullable Map<String, Object> model) {
this.request = request;
this.response = response;
this.model = model;
// Fetch WebApplicationContext, either from DispatcherServlet or the root context.
// ServletContext needs to be specified to be able to fall back to the root context!
WebApplicationContext wac = (WebApplicationContext) request.getAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE);
if (wac == null) {
wac = RequestContextUtils.findWebApplicationContext(request, servletContext);
if (wac == null) {
throw new IllegalStateException("No WebApplicationContext found: not in a DispatcherServlet " +
"request and no ContextLoaderListener registered?");
}
}
this.webApplicationContext = wac;
Locale locale = null;
TimeZone timeZone = null;
// Determine locale to use for this RequestContext.
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
if (localeResolver instanceof LocaleContextResolver) {
LocaleContext localeContext = ((LocaleContextResolver) localeResolver).resolveLocaleContext(request);
locale = localeContext.getLocale();
if (localeContext instanceof TimeZoneAwareLocaleContext) {
timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
}
}
else if (localeResolver != null) {
// Try LocaleResolver (we're within a DispatcherServlet request).
locale = localeResolver.resolveLocale(request);
}
this.locale = locale;
this.timeZone = timeZone;
// Determine default HTML escape setting from the "defaultHtmlEscape"
// context-param in web.xml, if any.
this.defaultHtmlEscape = WebUtils.getDefaultHtmlEscape(this.webApplicationContext.getServletContext());
// Determine response-encoded HTML escape setting from the "responseEncodedHtmlEscape"
// context-param in web.xml, if any.
this.responseEncodedHtmlEscape =
WebUtils.getResponseEncodedHtmlEscape(this.webApplicationContext.getServletContext());
this.urlPathHelper = new UrlPathHelper();
if (this.webApplicationContext.containsBean(RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME)) {
this.requestDataValueProcessor = this.webApplicationContext.getBean(
RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME, RequestDataValueProcessor.class);
}
}
/**
* Initialize this context with the given request, using the given model attributes for Errors retrieval.
* <p>Delegates to {@code getFallbackLocale} and {@code getFallbackTheme} for determining the fallback
* locale and theme, respectively, if no LocaleResolver and/or ThemeResolver can be found in the request.
* @param request current HTTP request
* @param servletContext the servlet context of the web application (can be {@code null}; necessary for
* fallback to root WebApplicationContext)
* @param model the model attributes for the current view (can be {@code null}, using the request attributes
* for Errors retrieval)
* @see #getFallbackLocale
* @see #getFallbackTheme
* @see org.springframework.web.servlet.DispatcherServlet#LOCALE_RESOLVER_ATTRIBUTE
* @see org.springframework.web.servlet.DispatcherServlet#THEME_RESOLVER_ATTRIBUTE
*/
protected void initContext(HttpServletRequest request, HttpServletResponse response, ServletContext servletContext,
Map<String, Object> model) {
this.request = request;
this.response = response;
this.model = model;
// Fetch WebApplicationContext, either from DispatcherServlet or the root context.
// ServletContext needs to be specified to be able to fall back to the root context!
this.webApplicationContext = (WebApplicationContext) request.getAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE);
if (this.webApplicationContext == null) {
this.webApplicationContext = RequestContextUtils.findWebApplicationContext(request, servletContext);
if (this.webApplicationContext == null) {
throw new IllegalStateException("No WebApplicationContext found: not in a DispatcherServlet " +
"request and no ContextLoaderListener registered?");
}
}
// Determine locale to use for this RequestContext.
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
if (localeResolver instanceof LocaleContextResolver) {
LocaleContext localeContext = ((LocaleContextResolver) localeResolver).resolveLocaleContext(request);
this.locale = localeContext.getLocale();
if (localeContext instanceof TimeZoneAwareLocaleContext) {
this.timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
}
}
else if (localeResolver != null) {
// Try LocaleResolver (we're within a DispatcherServlet request).
this.locale = localeResolver.resolveLocale(request);
}
// Try JSTL fallbacks if necessary.
if (this.locale == null) {
this.locale = getFallbackLocale();
}
if (this.timeZone == null) {
this.timeZone = getFallbackTimeZone();
}
// Determine default HTML escape setting from the "defaultHtmlEscape"
// context-param in web.xml, if any.
this.defaultHtmlEscape = WebUtils.getDefaultHtmlEscape(this.webApplicationContext.getServletContext());
// Determine response-encoded HTML escape setting from the "responseEncodedHtmlEscape"
// context-param in web.xml, if any.
this.responseEncodedHtmlEscape = WebUtils.getResponseEncodedHtmlEscape(this.webApplicationContext.getServletContext());
this.urlPathHelper = new UrlPathHelper();
if (this.webApplicationContext.containsBean(RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME)) {
this.requestDataValueProcessor = this.webApplicationContext.getBean(
RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME, RequestDataValueProcessor.class);
}
}
/**
* Initialize this context with the given request, using the given model attributes for Errors retrieval.
* <p>Delegates to {@code getFallbackLocale} and {@code getFallbackTheme} for determining the fallback
* locale and theme, respectively, if no LocaleResolver and/or ThemeResolver can be found in the request.
* @param request current HTTP request
* @param servletContext the servlet context of the web application (can be {@code null}; necessary for
* fallback to root WebApplicationContext)
* @param model the model attributes for the current view (can be {@code null}, using the request attributes
* for Errors retrieval)
* @see #getFallbackLocale
* @see #getFallbackTheme
* @see org.springframework.web.servlet.DispatcherServlet#LOCALE_RESOLVER_ATTRIBUTE
* @see org.springframework.web.servlet.DispatcherServlet#THEME_RESOLVER_ATTRIBUTE
*/
protected void initContext(HttpServletRequest request, HttpServletResponse response, ServletContext servletContext,
Map<String, Object> model) {
this.request = request;
this.response = response;
this.model = model;
// Fetch WebApplicationContext, either from DispatcherServlet or the root context.
// ServletContext needs to be specified to be able to fall back to the root context!
this.webApplicationContext = (WebApplicationContext) request.getAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE);
if (this.webApplicationContext == null) {
this.webApplicationContext = RequestContextUtils.findWebApplicationContext(request, servletContext);
if (this.webApplicationContext == null) {
throw new IllegalStateException("No WebApplicationContext found: not in a DispatcherServlet " +
"request and no ContextLoaderListener registered?");
}
}
// Determine locale to use for this RequestContext.
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
if (localeResolver instanceof LocaleContextResolver) {
LocaleContext localeContext = ((LocaleContextResolver) localeResolver).resolveLocaleContext(request);
this.locale = localeContext.getLocale();
if (localeContext instanceof TimeZoneAwareLocaleContext) {
this.timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
}
}
else if (localeResolver != null) {
// Try LocaleResolver (we're within a DispatcherServlet request).
this.locale = localeResolver.resolveLocale(request);
}
// Try JSTL fallbacks if necessary.
if (this.locale == null) {
this.locale = getFallbackLocale();
}
if (this.timeZone == null) {
this.timeZone = getFallbackTimeZone();
}
// Determine default HTML escape setting from the "defaultHtmlEscape"
// context-param in web.xml, if any.
this.defaultHtmlEscape = WebUtils.getDefaultHtmlEscape(this.webApplicationContext.getServletContext());
// Determine response-encoded HTML escape setting from the "responseEncodedHtmlEscape"
// context-param in web.xml, if any.
this.responseEncodedHtmlEscape = WebUtils.getResponseEncodedHtmlEscape(this.webApplicationContext.getServletContext());
this.urlPathHelper = new UrlPathHelper();
if (this.webApplicationContext.containsBean(RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME)) {
this.requestDataValueProcessor = this.webApplicationContext.getBean(
RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME, RequestDataValueProcessor.class);
}
}
/**
* 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 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()
*/
@Nullable
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;
}
/**
* 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 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()
*/
@Nullable
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;
}
/**
* 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 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;
}
/**
* 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;
}