Top Description Fields Constructors Methods
org.springframework.web.servlet.i18n

public Class SessionLocaleResolver

extends AbstractLocaleContextResolver
Class Inheritance
Imports
java.util.Locale, .TimeZone, java.util.function.Function, jakarta.servlet.http.HttpServletRequest, .HttpServletResponse, org.springframework.context.i18n.LocaleContext, .TimeZoneAwareLocaleContext, org.springframework.lang.Nullable, org.springframework.util.Assert, org.springframework.web.util.WebUtils

org.springframework.web.servlet.LocaleResolver implementation that uses a locale attribute in the user's session in case of a custom setting, with a fallback to the configured default locale, the request's Accept-Language header, or the default locale for the server.

This is most appropriate if the application needs user sessions anyway, i.e. when the HttpSession does not have to be created just for storing the user's locale. The session may optionally contain an associated time zone attribute as well; alternatively, you may specify a default time zone.

Custom controllers can override the user's locale and time zone by calling #setLocale(Context) on the resolver, e.g. responding to a locale change request. As a more convenient alternative, consider using org.springframework.web.servlet.support.RequestContext#changeLocale.

In contrast to CookieLocaleResolver, this strategy stores locally chosen locale settings in the Servlet container's HttpSession. As a consequence, those settings are just temporary for each session and therefore lost when each session terminates.

Note that there is no direct relationship with external session management mechanisms such as the "Spring Session" project. This LocaleResolver will simply evaluate and modify corresponding HttpSession attributes against the current HttpServletRequest.

Authors
Juergen Hoeller, Vedran Pavic
Since
27.02.2003
See Also
setDefaultLocale, setDefaultTimeZone

Field Summary

Modifier and TypeField and Description
private Function<HttpServletRequest, Locale>
private Function<HttpServletRequest, TimeZone>
public static final String
LOCALE_SESSION_ATTRIBUTE_NAME

Default name of the session attribute that holds the Locale.

private String
public static final String
TIME_ZONE_SESSION_ATTRIBUTE_NAME

Default name of the session attribute that holds the TimeZone.

private String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected Locale

Returns:

the default locale (never null)
determineDefaultLocale
(HttpServletRequest
the request to resolve the locale for
request
)
Deprecated since 6.0. as of 6.0, in favor of setDefaultLocaleFunction(Function)

Determine the default locale for the given request, called if no Locale session attribute has been found.

protected TimeZone

Returns:

the default time zone (or null if none defined)
determineDefaultTimeZone
(HttpServletRequest
the request to resolve the time zone for
request
)
Deprecated since 6.0. as of 6.0, in favor of setDefaultTimeZoneFunction(Function)

Determine the default time zone for the given request, called if no TimeZone session attribute has been found.

public Locale
public LocaleContext
resolveLocaleContext(final HttpServletRequest
the request to resolve the locale context for
request
)

Implements org.springframework.web.servlet.LocaleContextResolver.resolveLocaleContext.

Resolve the current locale context via the given request.

public void
setDefaultLocaleFunction(Function<HttpServletRequest, Locale>
the function used to determine the default locale
defaultLocaleFunction
)

Set the function used to determine the default locale for the given request, called if no Locale session attribute has been found.

public void
setDefaultTimeZoneFunction(Function<HttpServletRequest, TimeZone>
the function used to determine the default time zone
defaultTimeZoneFunction
)

Set the function used to determine the default time zone for the given request, called if no TimeZone session attribute has been found.

public void
setLocaleAttributeName(String localeAttributeName)

Specify the name of the corresponding attribute in the HttpSession, holding the current Locale value.

public void
setLocaleContext(HttpServletRequest
the request to be used for locale modification
request
,
HttpServletResponse
the response to be used for locale modification
response
,
LocaleContext
the new locale context, or null to clear the locale
localeContext
)

Implements org.springframework.web.servlet.LocaleContextResolver.setLocaleContext.

Set the current locale context to the given one, potentially including a locale with associated time zone information.

public void
setTimeZoneAttributeName(String timeZoneAttributeName)

Specify the name of the corresponding attribute in the HttpSession, holding the current TimeZone value.

Inherited from org.springframework.web.servlet.i18n.AbstractLocaleContextResolver:
getDefaultTimeZonesetDefaultTimeZone

Field Detail

defaultLocaleFunctionback to summary
private Function<HttpServletRequest, Locale> defaultLocaleFunction
defaultTimeZoneFunctionback to summary
private Function<HttpServletRequest, TimeZone> defaultTimeZoneFunction
LOCALE_SESSION_ATTRIBUTE_NAMEback to summary
public static final String LOCALE_SESSION_ATTRIBUTE_NAME

Default name of the session attribute that holds the Locale.

Only used internally by this implementation.

Use RequestContext(Utils).getLocale() to retrieve the current locale in controllers or views.

See Also
org.springframework.web.servlet.support.RequestContext#getLocale, org.springframework.web.servlet.support.RequestContextUtils#getLocale
localeAttributeNameback to summary
private String localeAttributeName
TIME_ZONE_SESSION_ATTRIBUTE_NAMEback to summary
public static final String TIME_ZONE_SESSION_ATTRIBUTE_NAME

Default name of the session attribute that holds the TimeZone.

Only used internally by this implementation.

Use RequestContext(Utils).getTimeZone() to retrieve the current time zone in controllers or views.

See Also
org.springframework.web.servlet.support.RequestContext#getTimeZone, org.springframework.web.servlet.support.RequestContextUtils#getTimeZone
timeZoneAttributeNameback to summary
private String timeZoneAttributeName

Constructor Detail

SessionLocaleResolverback to summary
public SessionLocaleResolver()

Method Detail

determineDefaultLocaleback to summary
protected Locale determineDefaultLocale(HttpServletRequest request)

Deprecated

since 6.0.

as of 6.0, in favor of setDefaultLocaleFunction(Function)

Determine the default locale for the given request, called if no Locale session attribute has been found.

The default implementation returns the configured default locale, if any, and otherwise falls back to the request's Accept-Language header locale or the default locale for the server.

Parameters
request:HttpServletRequest

the request to resolve the locale for

Returns:Locale

the default locale (never null)

Annotations
@Deprecated
since:6.0
See Also
setDefaultLocale, jakarta.servlet.http.HttpServletRequest#getLocale()
determineDefaultTimeZoneback to summary
protected TimeZone determineDefaultTimeZone(HttpServletRequest request)

Deprecated

since 6.0.

as of 6.0, in favor of setDefaultTimeZoneFunction(Function)

Determine the default time zone for the given request, called if no TimeZone session attribute has been found.

The default implementation returns the configured default time zone, if any, or null otherwise.

Parameters
request:HttpServletRequest

the request to resolve the time zone for

Returns:TimeZone

the default time zone (or null if none defined)

Annotations
@Deprecated
since:6.0
@Nullable
See Also
setDefaultTimeZone
resolveLocaleback to summary
public Locale resolveLocale(HttpServletRequest request)

Overrides default org.springframework.web.servlet.LocaleContextResolver.resolveLocale.

Implements org.springframework.web.servlet.LocaleResolver.resolveLocale.

Doc from org.springframework.web.servlet.LocaleContextResolver.resolveLocale.

Default implementation of LocaleResolver#resolveLocale(HttpServletRequest) that delegates to resolveLocaleContext(HttpServletRequest), falling back to HttpServletRequest#getLocale() if necessary.

Parameters
request:HttpServletRequest

the request to resolve the locale for

Returns:Locale

the current locale (never null)

Annotations
@Override
resolveLocaleContextback to summary
public LocaleContext resolveLocaleContext(final HttpServletRequest request)

Implements org.springframework.web.servlet.LocaleContextResolver.resolveLocaleContext.

Doc from org.springframework.web.servlet.LocaleContextResolver.resolveLocaleContext.

Resolve the current locale context via the given request.

This is primarily intended for framework-level processing; consider using org.springframework.web.servlet.support.RequestContextUtils or org.springframework.web.servlet.support.RequestContext for application-level access to the current locale and/or time zone.

The returned context may be a org.springframework.context.i18n.TimeZoneAwareLocaleContext, containing a locale with associated time zone information. Simply apply an instanceof check and downcast accordingly.

Custom resolver implementations may also return extra settings in the returned context, which again can be accessed through downcasting.

Parameters
request:HttpServletRequest

the request to resolve the locale context for

Returns:LocaleContext

the current locale context (never null

Annotations
@Override
setDefaultLocaleFunctionback to summary
public void setDefaultLocaleFunction(Function<HttpServletRequest, Locale> defaultLocaleFunction)

Set the function used to determine the default locale for the given request, called if no Locale session attribute has been found.

The default implementation returns the configured default locale, if any, and otherwise falls back to the request's Accept-Language header locale or the default locale for the server.

Parameters
defaultLocaleFunction:Function<HttpServletRequest, Locale>

the function used to determine the default locale

Since
6.0
See Also
setDefaultLocale, jakarta.servlet.http.HttpServletRequest#getLocale()
setDefaultTimeZoneFunctionback to summary
public void setDefaultTimeZoneFunction(Function<HttpServletRequest, TimeZone> defaultTimeZoneFunction)

Set the function used to determine the default time zone for the given request, called if no TimeZone session attribute has been found.

The default implementation returns the configured default time zone, if any, or null otherwise.

Parameters
defaultTimeZoneFunction:Function<HttpServletRequest, TimeZone>

the function used to determine the default time zone

Since
6.0
See Also
setDefaultTimeZone
setLocaleAttributeNameback to summary
public void setLocaleAttributeName(String localeAttributeName)

Specify the name of the corresponding attribute in the HttpSession, holding the current Locale value.

The default is an internal LOCALE_SESSION_ATTRIBUTE_NAME.

Since
4.3.8
setLocaleContextback to summary
public void setLocaleContext(HttpServletRequest request, HttpServletResponse response, LocaleContext localeContext)

Implements org.springframework.web.servlet.LocaleContextResolver.setLocaleContext.

Doc from org.springframework.web.servlet.LocaleContextResolver.setLocaleContext.

Set the current locale context to the given one, potentially including a locale with associated time zone information.

Parameters
request:HttpServletRequest

the request to be used for locale modification

response:HttpServletResponse

the response to be used for locale modification

localeContext:LocaleContext

the new locale context, or null to clear the locale

Annotations
@Override
setTimeZoneAttributeNameback to summary
public void setTimeZoneAttributeName(String timeZoneAttributeName)

Specify the name of the corresponding attribute in the HttpSession, holding the current TimeZone value.

The default is an internal TIME_ZONE_SESSION_ATTRIBUTE_NAME.

Since
4.3.8