SessionLocaleResolver 和 FixedLocaleResolver 之间的确切区别是什么
What is exact difference between SessionLocaleResolver and FixedLocaleResolver
我正在阅读 Java 中的本地化和国际化概念。
默认Locale
可以是SessionLocaleResolver
和FixedLocaleResolver
。
默认设置 SessionLocaleResolver
:
@Bean
public LocaleResolver localeResolver(){
SessionLocaleResolver r = new SessionLocaleResolver();
r.setDefaultLocale(Locale.US);
return r;
}
默认设置 FixedLocaleResolver
:
@Bean
public LocaleResolver localeResolver(){
FixedLocaleResolver r = new FixedLocaleResolver();
r.setDefaultLocale(Locale.US);
return r;
}
谁能告诉我这两者之间的确切区别是什么,这种区别对申请有何影响?
以下是我们可以从 Spring 文档中找到的内容
FixedLocaleResolver:
LocaleResolver implementation that always returns a fixed default
locale and optionally time zone. Default is the current JVM's default
locale.
SessionLocaleResolver:
LocaleResolver implementation that uses a locale attribute in the
user's session in case of a custom setting, with a fallback to the
specified default locale or the request's accept-header locale. 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.
通常当你想在你的 Spring 引导应用程序中启用国际化功能时你想在你的配置中定义第二个@Bean class:
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
lci.setParamName("lang");
return lci;
}
这与 SessionLocaleResolver 一起将使客户端有可能向您的应用程序发送带有“lang”参数的任何请求,例如:
localhost:8080/public/api/?lang=pt_BR
结果会将会话语言环境属性从默认 en_US 设置为 pt_BR,然后可用于更改网页上显示的语言。
另一方面,如果您将使用 FixedLocaleResolver 并保留 LocaleChangeInterceptor 不变,那么当客户端发送带有“lang”参数的请求时,服务器将响应 Http状态 500 代码 - 内部服务器错误,您将在控制台中看到抛出的异常:
UnsupportedOperationException: Cannot change fixed locale - use a
different locale resolution strategy
所以 FixedLocaleResolver 基本上适用于您想要预先禁用更改应用程序中区域设置的可能性的情况。
我正在阅读 Java 中的本地化和国际化概念。
默认Locale
可以是SessionLocaleResolver
和FixedLocaleResolver
。
默认设置 SessionLocaleResolver
:
@Bean
public LocaleResolver localeResolver(){
SessionLocaleResolver r = new SessionLocaleResolver();
r.setDefaultLocale(Locale.US);
return r;
}
默认设置 FixedLocaleResolver
:
@Bean
public LocaleResolver localeResolver(){
FixedLocaleResolver r = new FixedLocaleResolver();
r.setDefaultLocale(Locale.US);
return r;
}
谁能告诉我这两者之间的确切区别是什么,这种区别对申请有何影响?
以下是我们可以从 Spring 文档中找到的内容
FixedLocaleResolver:
LocaleResolver implementation that always returns a fixed default locale and optionally time zone. Default is the current JVM's default locale.
SessionLocaleResolver:
LocaleResolver implementation that uses a locale attribute in the user's session in case of a custom setting, with a fallback to the specified default locale or the request's accept-header locale. 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.
通常当你想在你的 Spring 引导应用程序中启用国际化功能时你想在你的配置中定义第二个@Bean class:
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
lci.setParamName("lang");
return lci;
}
这与 SessionLocaleResolver 一起将使客户端有可能向您的应用程序发送带有“lang”参数的任何请求,例如:
localhost:8080/public/api/?lang=pt_BR
结果会将会话语言环境属性从默认 en_US 设置为 pt_BR,然后可用于更改网页上显示的语言。
另一方面,如果您将使用 FixedLocaleResolver 并保留 LocaleChangeInterceptor 不变,那么当客户端发送带有“lang”参数的请求时,服务器将响应 Http状态 500 代码 - 内部服务器错误,您将在控制台中看到抛出的异常:
UnsupportedOperationException: Cannot change fixed locale - use a different locale resolution strategy
所以 FixedLocaleResolver 基本上适用于您想要预先禁用更改应用程序中区域设置的可能性的情况。