如何使用LocalValueBean 解析html:optionCollection in struts 中带有一些UTF8 字符的字符串?
How to parse the string with some UTF8 characters in the html:optionCollection in struts using LocalValueBean?
我已阅读资源文件并使用 html:optionsCollection 创建了下拉列表。但无法解析屏幕截图中下拉选择中显示的 UTF8 字符串。这应该解析为 Français(加拿大)
截图显示:
示例代码如下
JSP代码:
<html:select property="locale" onchange="refreshPage(this.value)">
<html:optionsCollection property="dropDownList"/>
</html:select>
动作表单 bean class
public void setDropDownList(Collection dropDownList){
this.dropDownList = dropDownList;
}
public Collection getDropDownList(){
if(dropDownList == null){
dropDownList = new Vector(10);
Locale availableLanguages[] = DAOFactory.getConfigurationDAO().getAvailableLanguages();
Properties properties = loadProperties(Locale.ENGLISH);
for(int i=0 ; i<availableLanguages.length ; i++){
String localeNameKey = "com.web.ui.localeName." + availableLanguages[i].toString();
String value = availableLanguages[i].toString();
String key = properties.getProperty(localeNameKey);
dropDownList.add(new LabelValueBean(key,value));
}
log.debug("size of dropdown :: "+dropDownList.size());
}
return dropDownList;
}
private Properties loadProperties(Locale locale) {
try {
InputStream inputStream = null;
inputStream = LoginForm.class
.getResourceAsStream("/resources/application_"
+ locale.getLanguage() + "_" + locale.getCountry()
+ ".properties");
if (inputStream == null) {
inputStream = LoginForm.class
.getResourceAsStream("/resources/application.properties");
}
if (inputStream == null) {
throw new IllegalStateException(
"Unable to load the application properties.");
}
//BufferedReader buffRead = new BufferedReader(new InputStreamReader(inputStream));
Properties p = new Properties();
p.load(inputStream);
return p;
} catch (IOException e) {
throw new IllegalStateException(
"Was not able to load the application properties.");
}
}
感谢任何帮助。
JSP 带有脚本的代码:
<html:select property="locale" onchange="refreshAndSetValues(this.value)">
<% Locale availableLanguages[] = DAOFactory.getConfigurationDAO().getAvailableLanguages();
for (int i=0; i<availableLanguages.length; i++) {
String localeNameKey = "com.web.gui.localeName." + availableLanguages[i].toString(); %>
<html:option value="<%= availableLanguages[i].toString() %>"
key="<%= localeNameKey %>"/>
<% }%>
</html:select>
Scriptlet 代码读取属性文件并成功解析这些字符串。
属性文件
com.web.ui.locale=Language
com.web.ui.localeName.en_US=English
com.web.ui.localeName.fr_CA=Français (Canada)
com.web.ui.localeName.fr_CA.decoded,Français (Canada)
com.web.ui.localeName.fr_FR=Français (France),
com.web.ui.localeName.nl_NL=Nederlands
com.web.ui.localeName.es_419=Español (América Latina)
com.web.ui.localeName.es_ES=Español (Castellano)
com.web.ui.localeName.de_DE=Deutsch
嗯,您的问题就在您的属性文件中:
com.web.ui.localeName.fr_CA=Français (Canada)
相比于:
com.web.ui.localeName.fr_FR=Français (France),
由于文件中有转义符,而 JSP 转义了转义符,因此应删除转义符并从 France 行复制单词。
我已阅读资源文件并使用 html:optionsCollection 创建了下拉列表。但无法解析屏幕截图中下拉选择中显示的 UTF8 字符串。这应该解析为 Français(加拿大)
截图显示:
示例代码如下
JSP代码:
<html:select property="locale" onchange="refreshPage(this.value)">
<html:optionsCollection property="dropDownList"/>
</html:select>
动作表单 bean class
public void setDropDownList(Collection dropDownList){
this.dropDownList = dropDownList;
}
public Collection getDropDownList(){
if(dropDownList == null){
dropDownList = new Vector(10);
Locale availableLanguages[] = DAOFactory.getConfigurationDAO().getAvailableLanguages();
Properties properties = loadProperties(Locale.ENGLISH);
for(int i=0 ; i<availableLanguages.length ; i++){
String localeNameKey = "com.web.ui.localeName." + availableLanguages[i].toString();
String value = availableLanguages[i].toString();
String key = properties.getProperty(localeNameKey);
dropDownList.add(new LabelValueBean(key,value));
}
log.debug("size of dropdown :: "+dropDownList.size());
}
return dropDownList;
}
private Properties loadProperties(Locale locale) {
try {
InputStream inputStream = null;
inputStream = LoginForm.class
.getResourceAsStream("/resources/application_"
+ locale.getLanguage() + "_" + locale.getCountry()
+ ".properties");
if (inputStream == null) {
inputStream = LoginForm.class
.getResourceAsStream("/resources/application.properties");
}
if (inputStream == null) {
throw new IllegalStateException(
"Unable to load the application properties.");
}
//BufferedReader buffRead = new BufferedReader(new InputStreamReader(inputStream));
Properties p = new Properties();
p.load(inputStream);
return p;
} catch (IOException e) {
throw new IllegalStateException(
"Was not able to load the application properties.");
}
}
感谢任何帮助。
JSP 带有脚本的代码:
<html:select property="locale" onchange="refreshAndSetValues(this.value)">
<% Locale availableLanguages[] = DAOFactory.getConfigurationDAO().getAvailableLanguages();
for (int i=0; i<availableLanguages.length; i++) {
String localeNameKey = "com.web.gui.localeName." + availableLanguages[i].toString(); %>
<html:option value="<%= availableLanguages[i].toString() %>"
key="<%= localeNameKey %>"/>
<% }%>
</html:select>
Scriptlet 代码读取属性文件并成功解析这些字符串。
属性文件
com.web.ui.locale=Language
com.web.ui.localeName.en_US=English
com.web.ui.localeName.fr_CA=Français (Canada)
com.web.ui.localeName.fr_CA.decoded,Français (Canada)
com.web.ui.localeName.fr_FR=Français (France),
com.web.ui.localeName.nl_NL=Nederlands
com.web.ui.localeName.es_419=Español (América Latina)
com.web.ui.localeName.es_ES=Español (Castellano)
com.web.ui.localeName.de_DE=Deutsch
嗯,您的问题就在您的属性文件中:
com.web.ui.localeName.fr_CA=Français (Canada)
相比于:
com.web.ui.localeName.fr_FR=Français (France),
由于文件中有转义符,而 JSP 转义了转义符,因此应删除转义符并从 France 行复制单词。