0

I have a problem. I followed instruction in this tutorial. But I have a problem. When I change language from selectOneMenu, nothing happend. But default language it´s load good. This is my xhtml file:

   <?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://xmlns.jcp.org/jsf/html"
          xmlns:f="http://xmlns.jcp.org/jsf/core">
        <h:head>
            <title>Letecký systém</title>
            <h:outputStylesheet library="css" name="login.css"  />
        </h:head>
        <h:body>
            <div id="nadpis">Letecký systém</div>
            <h:panelGrid columns="2">

                Language : 
                <h:selectOneMenu value="#{mc.localeCode}" onchange="submit()"
                                 valueChangeListener="#{mc.countryLocaleCodeChanged}">
                    <f:selectItems value="#{mc.countriesInMap}" /> 
                </h:selectOneMenu>
            </h:panelGrid> 
            <div id="mainForm">
            .
            .
            .
            .
            </div>
        </h:body> </html>

And this is my controller:

@ManagedBean(name = "mc")
@SessionScoped
public class MainController {

    private static final long serialVersionUID = 1L;

                .
                .
                .
                .

    //Translate
    private String localeCode;

    private static Map<String, Object> countries;

    static {
        countries = new LinkedHashMap<String, Object>();
        countries.put("English", Locale.ENGLISH); //label, value
        countries.put("French", Locale.FRENCH);
    }

    public Map<String, Object> getCountriesInMap() {
        return countries;
    }

    public String getLocaleCode() {
        return localeCode;
    }

    public void setLocaleCode(String localeCode) {
        this.localeCode = localeCode;
    }

    public void countryLocaleCodeChanged(ValueChangeEvent e) {

        String newLocaleValue = e.getNewValue().toString();

        for (Map.Entry<String, Object> entry : countries.entrySet()) {

            if (entry.getValue().toString().equals(newLocaleValue)) {

                FacesContext.getCurrentInstance()
                        .getViewRoot().setLocale((Locale) entry.getValue());

            }
        }

    }

And finally this is my faces-config.xml:

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
              xmlns="http://xmlns.jcp.org/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
    <application>
        <locale-config>
            <default-locale>en</default-locale>
            <supported-locale>fr</supported-locale>
        </locale-config>
        <resource-bundle>
            <base-name>resources.messages</base-name>
            <var>msg</var>
        </resource-bundle>
    </application>
</faces-config>

My program has structure

  • Web Pages -> index.xhtml
  • Source packages
    • controller -> MainController.java
    • resources -> messages.properties -> messages_fr.properties
  • Configuration Files ->faces-config.xml

Where is a problem?

Kristián Stroka
  • 698
  • 1
  • 8
  • 23
  • There was a problem, that in XML selectOneMenu must be in form. After that it´s working correctly :) – Kristián Stroka May 20 '15 at 20:56
  • 1
    As an advice, never use tutorialspoint, javabeat or roseindia. They are all low quality as they are maintained by amateurs with sole purpose to generate ads income. Use mkyong or of course stack overflow via jsf.zeef.com. – BalusC May 20 '15 at 22:42

0 Answers0