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

public Class LocaleChangeInterceptor

extends Object
implements HandlerInterceptor
Class Inheritance
All Implemented Interfaces
org.springframework.web.servlet.HandlerInterceptor
Imports
java.util.Locale, jakarta.servlet.ServletException, jakarta.servlet.http.HttpServletRequest, .HttpServletResponse, org.apache.commons.logging.Log, .LogFactory, org.springframework.lang.Nullable, org.springframework.util.ObjectUtils, .StringUtils, org.springframework.web.servlet.HandlerInterceptor, .LocaleResolver, org.springframework.web.servlet.support.RequestContextUtils

Interceptor that allows for changing the current locale on every request, via a configurable request parameter (default parameter name: "locale").
Authors
Juergen Hoeller, Rossen Stoyanchev
Since
20.06.2003
See Also
org.springframework.web.servlet.LocaleResolver

Field Summary

Modifier and TypeField and Description
public static final String
DEFAULT_PARAM_NAME

Default name of the locale specification parameter: "locale".

private String[]
private boolean
protected final Log
private String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private boolean
checkHttpMethod(String currentMethod)

public String[]
getHttpMethods()

Return the configured HTTP methods.

public String
getParamName()

Return the name of the parameter that contains a locale specification in a locale change request.

public boolean
isIgnoreInvalidLocale()

Return whether to ignore an invalid value for the locale parameter.

protected Locale

Returns:

the corresponding Locale instance
parseLocaleValue
(String
the locale value to parse
localeValue
)

Parse the given locale value as coming from a request parameter.

public boolean
preHandle(HttpServletRequest
current HTTP request
request
,
HttpServletResponse
current HTTP response
response
,
Object
chosen handler to execute, for type and/or instance evaluation
handler
)

Overrides default org.springframework.web.servlet.HandlerInterceptor.preHandle.

Interception point before the execution of a handler.

public void
setHttpMethods(String...
the methods
httpMethods
)

Configure the HTTP method(s) over which the locale can be changed.

public void
setIgnoreInvalidLocale(boolean ignoreInvalidLocale)

Set whether to ignore an invalid value for the locale parameter.

public void
setParamName(String paramName)

Set the name of the parameter that contains a locale specification in a locale change request.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DEFAULT_PARAM_NAMEback to summary
public static final String DEFAULT_PARAM_NAME

Default name of the locale specification parameter: "locale".

httpMethodsback to summary
private String[] httpMethods
Annotations
@Nullable
ignoreInvalidLocaleback to summary
private boolean ignoreInvalidLocale
loggerback to summary
protected final Log logger
paramNameback to summary
private String paramName

Constructor Detail

LocaleChangeInterceptorback to summary
public LocaleChangeInterceptor()

Method Detail

checkHttpMethodback to summary
private boolean checkHttpMethod(String currentMethod)
getHttpMethodsback to summary
public String[] getHttpMethods()

Return the configured HTTP methods.

Annotations
@Nullable
Since
4.2
getParamNameback to summary
public String getParamName()

Return the name of the parameter that contains a locale specification in a locale change request.

isIgnoreInvalidLocaleback to summary
public boolean isIgnoreInvalidLocale()

Return whether to ignore an invalid value for the locale parameter.

Since
4.2.2
parseLocaleValueback to summary
protected Locale parseLocaleValue(String localeValue)

Parse the given locale value as coming from a request parameter.

The default implementation calls StringUtils#parseLocale(String), accepting the Locale#toString format as well as BCP 47 language tags.

Parameters
localeValue:String

the locale value to parse

Returns:Locale

the corresponding Locale instance

Annotations
@Nullable
Since
4.3
preHandleback to summary
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException

Overrides default org.springframework.web.servlet.HandlerInterceptor.preHandle.

Doc from org.springframework.web.servlet.HandlerInterceptor.preHandle.

Interception point before the execution of a handler. Called after HandlerMapping determined an appropriate handler object, but before HandlerAdapter invokes the handler.

DispatcherServlet processes a handler in an execution chain, consisting of any number of interceptors, with the handler itself at the end. With this method, each interceptor can decide to abort the execution chain, typically sending an HTTP error or writing a custom response.

Note

special considerations apply for asynchronous request processing. For more details see org.springframework.web.servlet.AsyncHandlerInterceptor.

The default implementation returns true.

Parameters
request:HttpServletRequest

current HTTP request

response:HttpServletResponse

current HTTP response

handler:Object

chosen handler to execute, for type and/or instance evaluation

Returns:boolean

true if the execution chain should proceed with the next interceptor or the handler itself. Else, DispatcherServlet assumes that this interceptor has already dealt with the response itself.

Annotations
@Override
setHttpMethodsback to summary
public void setHttpMethods(String... httpMethods)

Configure the HTTP method(s) over which the locale can be changed.

Parameters
httpMethods:String[]

the methods

Since
4.2
setIgnoreInvalidLocaleback to summary
public void setIgnoreInvalidLocale(boolean ignoreInvalidLocale)

Set whether to ignore an invalid value for the locale parameter.

Since
4.2.2
setParamNameback to summary
public void setParamName(String paramName)

Set the name of the parameter that contains a locale specification in a locale change request. Default is "locale".