How to parse the string with some UTF8 characters in the html:optionCollection in struts using LocalValueBean?

243 views Asked by At

I have read the resource file and created the dropdownlist using html:optionsCollection. But unable to parse the UTF8 string showing in the dropdown selection in screenshot. This should parse as Français (Canada)

Showing in the screenshot : enter image description here

Following is the sample code

JSP Code :

<html:select property="locale" onchange="refreshPage(this.value)">
                                <html:optionsCollection property="dropDownList"/>
</html:select>

Action Form 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.");
                }
        }

Any help is appreciated.

JSP code with scriptlets :

<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 code read the properties file and parse those strings successfully.

Properties file

com.web.ui.locale=Language
com.web.ui.localeName.en_US=English
com.web.ui.localeName.fr_CA=Fran&ccedil;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
1

There are 1 answers

0
RealSkeptic On BEST ANSWER

Well, your problem is right there in your properties file:

com.web.ui.localeName.fr_CA=Fran&ccedil;ais (Canada)

Compared to:

com.web.ui.localeName.fr_FR=Français (France),

Since you have an escape in the file, and the JSP escapes the escape, you should remove the escape and copy the word from the France line.