我正在使用 Spring 3.1 并希望为当前用户找到事件的语言环境有没有一种方法可以直接获取语言环境而无需将其从 Controller 传递到服务层......等等。
spring 是否将语言环境存储在线程本地存储中,可以通过调用一些静态方法来获取它?
最佳答案
我也在寻找如何在不传递 Locale
的情况下访问语言环境,但我对 Spring 还是很陌生,发现大多数解决方案都令人困惑。但事实证明,Spring MVC 确实将语言环境存储在线程本地存储中。它可以通过以下方式访问:
Locale locale = LocaleContextHolder.getLocale();
The last approach [...] is based on a thread local in order to provide the current locale in any entity of your architecture. [...] You must be aware that the content of the LocaleContextHolder corresponds by default to the locale specified within the Web request.
发件人:Configuring locale switching with Spring MVC 3 . (该帖子还提供了获取语言环境的替代配置/方法,这可能对任何想要这样做的人有用)。
您还可以查看 Spring here 中的 LocaleContextHolder
文档.
关于spring - 如何从 Spring 获取当前用户语言环境而不将其作为参数传递给函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10792551/