For this example, we are still referring to previous source code (Please refer to part 1 of the JSF tutorial to download the basic code structure and help you to get started):
Below is the folder structure and the recent files that has been added for a more visualize presentation
New Files:
messages_fr.properties
==========================
hello.world.name=Name fr
hello.world.header=This is a simple Hello World example fr
hello.world.enter.name=Please enter your name fr
hello.world.select.language=Please select language fr
hello.world.location.param=Location fr: {0}
hello.world.title.login.page=Login Page fr
hello.world.btn.press.me=Press me fr
I just put additional string to be able to know if our simple example, has been successfully invoke, which in this case, I added the "fr" string at the end of every label
Modified files:
HelloWorld.java
package com.lai;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Locale;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
@ManagedBean
@SessionScoped
public class HelloWorld implements Serializable{
private ArrayList<SelectItem> localeLst; //variable for the list of locales
private String selectedLanguage;
/**
* default empty constructor
*/
public HelloWorld(){
System.out.println("############# INITIALIZING ################");
initialize();
}
private void initialize(){
populateLocaleList();
}
//Helper Methods
//Populate Locales
private void populateLocaleList(){
if(localeLst == null){
localeLst = new ArrayList<SelectItem>();
localeLst.add(new SelectItem( "en", "English") );
localeLst.add(new SelectItem( "de", "Germany") );
localeLst.add(new SelectItem( "fr", "French") );
}
}
//Method to be called, once a new locale has been selected
public void changeLanguageEventListener(ValueChangeEvent e){
FacesContext.getCurrentInstance().getViewRoot().setLocale(new Locale(e.getNewValue().toString()));
}
// -------------------
// -- getter & setter
// -------------------
public ArrayList<SelectItem> getLocaleLst() {
if(localeLst == null){
populateLocaleList();
}
return localeLst;
}
public void setLocaleLst(ArrayList<SelectItem> localeLst) {
this.localeLst = localeLst;
}
public String getSelectedLanguage() {
return selectedLanguage;
}
public void setSelectedLanguage(String selectedLanguage) {
this.selectedLanguage = selectedLanguage;
}
}
helloWorld.xhtml
. . .
<h:outputLabel for="name" value="#{msgs['hello.world.name']}" />
<h:selectOneMenu value="helloWorld.selectedLanguage"
onchange="submit()"
valueChangeListener="#{helloWorld.changeLanguageEventListener}"
>
<f:selectItems value="#{helloWorld.localeLst}"
var="c" itemLabel="#{c.key}" itemValue="#{c.value}"
/>
</h:selectOneMenu>
. . .
faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<application>
<locale-config>
<default-locale>en</default-locale>
</locale-config>
<resource-bundle>
<base-name>messages</base-name>
<var>msgs</var>
</resource-bundle>
</application>
</faces-config>
Compile the source code by: mvn clean install
copy the new war file to the glassfish server
restart the server
URL: http://localhost:8080/prjJsf/helloWorld.faces
Output:
Using tag as the EventListener
helloWorld.xhtml
...
%lt;h:selectOneMenu value="#{helloWorld.selectedLanguage}"
onchange="submit()"%gt;
%lt;f:valueChangeListener type="com.lai.HelloWorld"/%gt;
%lt;f:selectItems value="#{helloWorld.localeLst}"
var="c" itemLabel="#{c.key}" itemValue="#{c.value}"
/%gt;
%lt;/h:selectOneMenu%gt;
...
Note that we emphasize the valueChangeListener as tag (f:valueChangeListener)
HelloWorld.java
. . .
import javax.faces.event.ValueChangeListener;
public class HelloWorld implements ValueChangeListener, Serializable{
public void processValueChange(ValueChangeEvent e){
FacesContext.getCurrentInstance().getViewRoot().setLocale(new Locale(e.getNewValue().toString()));
}
. . .
We implements the ValueChangeListener interface.
After build and run, it will behave exactly the same, but just using different approach for the EventListener.




