最近項目部署到Linux環境下,出現部分國際化資源文件內容展現爲英文問題,出現該問題後感到很是奇怪。html
以前項目也有部署到Linux環境下的狀況,都並未出現該問題,仔細檢查Linux環境變量也沒問題。java
起初覺得Spring獲取Locale信息,應該是根據請求頭來進行判斷,因此又檢查瀏覽器請求信息同時設置默認信息linux
後面請求頭髮現請求也沒問題,這種時候只能對源碼進行查看了。
api
仔細檢查代碼後發現出現英文亂碼的地方都是使用Spring的上下文進行資源文件獲取。瀏覽器
而Spring對於locale獲取的時候,若是用戶未傳遞該值則是默認Java 虛擬機實例的當前默認語言環境值。oracle
而以前已經肯定linux環境已經爲中文環境,而爲何獲取到的語言環境又是英文呢。難道是虛擬機的問題?app
因而又查看JVM虛擬機的配置信息。less
經過jps命令能夠清楚看到當前jvm虛擬機的詳細的信息,同時也發現了確實是虛擬機出現的問題。jvm
可是爲何頁面struts顯示沒有問題呢,因而又繼續檢查代碼。ide
public Locale getLocale() { ActionContext ctx = ActionContext.getContext(); if (ctx != null) { return ctx.getLocale(); } else { if (LOG.isDebugEnabled()) { LOG.debug("Action context not initialized"); } return null; } }
struts經過ActionContext進行獲取,而ActionContext的信息又是來自於哪裏呢?
public static ActionContext getActionContext(HttpServletRequest req) { ValueStack vs = getValueStack(req); return vs != null?new ActionContext(vs.getContext()):null; }
struts 的locale信息來自於request的請求中,因此該字段確定不會爲空,而訪問的請求確定是中文環境,因此頁面顯示沒有問題,而Spring 因爲沒有傳遞locale對象,則直接獲取虛擬機的語言環境,因此出現了部分信息出現亂碼問題。
既然找到了問題就好處理,針對JVM虛擬機進行默認語言環境設置便可。
The default locale of your application is determined in three ways. First, unless you have explicitly changed the default, the
Locale.getDefault()
method returns the locale that was initially determined by the Java Virtual Machine (JVM) when it first loaded. That is, the JVM determines the default locale from the host environment. The host environment's locale is determined by the host operating system and the user preferences established on that system.Second, on some Java runtime implementations, the application user can override the host's default locale by providing this information on the command line by setting the
user.language
,user.country
, anduser.variant
system properties.Third, your application can call the
Locale.setDefault(Locale)
method. The setDefault(Locale aLocale) method lets your application set a systemwide (actually VM-wide) resource. After you set the default locale with this method, subsequent calls to Locale.getDefault() will return the newly set locale.
修改catalina.sh ,添加默認語言環境設置,保存重啓,問題解決!
可是爲何系統已是中文環境,jvm獲取到的數據仍是英文的環境呢,初次加載的時候系統環境語言爲英文,後續修改jvm後但沒有手工去更改語言環境,致使仍是獲取的第一次加載的環境。
以後諮詢售後的同事,發現現場確實是這樣,以後再在中文環境下安裝的程序就未出現該問題。