当我在web-inf文件夹中有多个.properties文件时,如何使用servlet获取适当的.properties文件?

时间:2021-07-05 17:17:53
<param-name>Language</param-name>
    <param-value>en</param-value>
</context-param>
<context-param>
    <param-name>Country</param-name>
    <param-value>US</param-value>
</context-param>
<context-param>
    <param-name>Language</param-name>
    <param-value>jp</param-value>
</context-param>
<context-param>
    <param-name>Country</param-name>
    <param-value>JP</param-value>
</context-param>

this is my web.xml file code.

这是我的web.xml文件代码。

ServletContext app = getServletContext();
        localeLang = app.getInitParameter("Language");
        localeCountry = app.getInitParameter("Country"); 

this is my jsp code where i'm accessing properties files based on language and country. i.e., when lang is en and country is US i need to access en_US.properties file and when lang is jp and Country is JP i need to access jp_JP.properties file like that when i have 100 properties file then how can i access properties file based on appropriate lang and country?

这是我的jsp代码,我根据语言和国家访问属性文件。即,当lang是en而country是US时我需要访问en_US.properties文件,当lang是jp而Country是JP时我需要访问jp_JP.properties文件,当我有100个属性文件时,我怎样才能访问属性文件基于适当的lang和国家?

1 个解决方案

#1


0  

Rather than directly accessing the properties files you should consider using the ResourceBundle mechanism, which is designed for precisely this situation. Put the properties files in WEB-INF/classes instead of just in WEB-INF, and name them all with a common prefix

您不应直接访问属性文件,而应考虑使用ResourceBundle机制,该机制专为此情况而设计。将属性文件放在WEB-INF / classes中而不是仅放在WEB-INF中,并使用公共前缀将它们全部命名

messages.properties
messages_en.properties
messages_en_GB.properties
messages_jp.properties

Use ResourceBundle.getBundle("messages", new Locale(localeLang, localeCountry)) and it will pull properties from messages_lang_country.properties when they exist in there, and fall back to messages_lang.properties and messages.properties if necessary.

使用ResourceBundle.getBundle(“messages”,新的Locale(localeLang,localeCountry)),它将在messages_lang_country.properties中存在属性,并在必要时回退到messages_lang.properties和messages.properties。

#1


0  

Rather than directly accessing the properties files you should consider using the ResourceBundle mechanism, which is designed for precisely this situation. Put the properties files in WEB-INF/classes instead of just in WEB-INF, and name them all with a common prefix

您不应直接访问属性文件,而应考虑使用ResourceBundle机制,该机制专为此情况而设计。将属性文件放在WEB-INF / classes中而不是仅放在WEB-INF中,并使用公共前缀将它们全部命名

messages.properties
messages_en.properties
messages_en_GB.properties
messages_jp.properties

Use ResourceBundle.getBundle("messages", new Locale(localeLang, localeCountry)) and it will pull properties from messages_lang_country.properties when they exist in there, and fall back to messages_lang.properties and messages.properties if necessary.

使用ResourceBundle.getBundle(“messages”,新的Locale(localeLang,localeCountry)),它将在messages_lang_country.properties中存在属性,并在必要时回退到messages_lang.properties和messages.properties。