I'm using JSF 2.2.13 and PrimeFaces 6.1.1 in my project. And trying to create primefaces selectOneMenu which changes locale.But when valueChangeListener is fired - ValueChangeEvent always has null newValue.
Here is my selectOneMenu:
<p:selectOneMenu value="#{localeBean.localeCode}" id="lang" valueChangeListener="#{localeBean.valueChangeListener}">
<f:selectItem itemValue="pl" itemLabel="${msg['page.login.language.pl.label']}"/>
<f:selectItem itemValue="en" itemLabel="${msg['page.login.language.en.label']}"/>
<p:ajax event="itemSelect" update="@this"/>
</p:selectOneMenu>
And my LocaleBean:
@Named
@SessionScoped
public class LocaleBean implements ILocaleBean, Serializable {
private String localeCode;
private Locale locale;
private boolean initiated = false;
private static final Log log = LogFactory.getLog(LocaleBean.class);
@PostConstruct
public void init() {
setDefaultLocale();
this.localeCode = locale.getLanguage();
initiated = true;
}
public void setLocaleCode(String localeCode) {
this.localeCode = localeCode;
}
public String getLocaleCode() {
if (!initiated) {
init();
}
return getLocale().toString();
}
public void valueChangeListener(ValueChangeEvent event) {
setLocaleFromString(event.getNewValue().toString());
}
@Override
public Locale getLocale() {
return locale;
}
}
You should use event="valueChange"
instead of event="itemSelect"
or leave it empty as suggested by Holger.
Or as the documentation states:
If no event is specific the default event is called. In addition to the standard events like "change", custom "itemSelect" event is also available to invoke when an item is selected from dropdown.
Default Event: valueChange
When selection happens, the selected value has not yet been submitted, so the listener will not be able to see the new value.