ValueChangeEvent 没有得到 newValue
ValueChangeEvent doesn't get newValue
我在我的项目中使用 JSF 2.2.13 和 PrimeFaces 6.1.1。并尝试创建 primefaces selectOneMenu
当 valueChangeListener 被触发时,它会改变 locale.But - ValueChangeEvent 总是有 null newValue.
ValeuChangeEvent:
这是我的 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>
还有我的 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;
}
}
您应该使用 event="valueChange"
而不是 event="itemSelect"
或按照 Holger 的建议将其留空。
或者如文档所述:
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
选择发生时,选择的值还没有提交,所以监听器将无法看到新的值。
我在我的项目中使用 JSF 2.2.13 和 PrimeFaces 6.1.1。并尝试创建 primefaces selectOneMenu 当 valueChangeListener 被触发时,它会改变 locale.But - ValueChangeEvent 总是有 null newValue.
ValeuChangeEvent:
这是我的 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>
还有我的 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;
}
}
您应该使用 event="valueChange"
而不是 event="itemSelect"
或按照 Holger 的建议将其留空。
或者如文档所述:
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
选择发生时,选择的值还没有提交,所以监听器将无法看到新的值。