将空值赋值给绑定的实例时遇到问题。每当在selectOneMenu中进行选择时,一切都会按预期进行(注册为cc.attrs.city的实例被设置为选定的值,如下面的示例所示),但是,当选择硬编码的空值时,我会检查我的选择是否没有转发到绑定的实例。任何帮助都会得到重视。
使用示例:
<h:selectOneMenu id="city" value="#{cc.attrs.city}"
converter="anySelectConverter">
<f:selectItem itemLabel="#{msg['general.choose']}" itemValue="#{null}"/>
<f:selectItems itemLabel="#{city.name}" itemValue="#{city}"
var="city" value="#{locationComponentController.getAllCities()}" />
<p:ajax event="valueChange" update="district subdistrict village neighbourhood #{cc.attrs.updateOnSelection}" />
</h:selectOneMenu>任何选择转换器:
@FacesConverter("anySelectConverter")
public class AnySelectConverter implements Converter{
private static Map<Object, String> entities = new ConcurrentHashMap<Object, String>();
@Override
public String getAsString(FacesContext context, UIComponent component, Object entity) {
// TODO : Geçici çözüm sebebi araştırılmalı
if(entity == null)
return "";
synchronized (entities) {
if (!entities.containsKey(entity)) {
String uuid = UUID.randomUUID().toString();
entities.put(entity, uuid);
return uuid;
} else {
return entities.get(entity);
}
}
}
@Override
public Object getAsObject(FacesContext context, UIComponent component, String uuid) {
for (Entry<Object, String> entry : entities.entrySet()) {
if (entry.getValue().equals(uuid)) {
return entry.getKey();
}
}
return null;
}
}发布于 2014-10-30 22:36:28
尝试将null项指定为noSelectionOption,因为看起来这是您的意图:
<h:selectOneMenu id="city" value="#{cc.attrs.city}"
converter="anySelectConverter">
<f:selectItem itemLabel="#{msg['general.choose']}" itemValue="#{null}" noSelectionOption="true"/>
<f:selectItems itemLabel="#{city.name}" itemValue="#{city}"
var="city" value="#{locationComponentController.getAllCities()}" />
<p:ajax event="valueChange" update="district subdistrict village neighbourhood #{cc.attrs.updateOnSelection}" />
</h:selectOneMenu>https://stackoverflow.com/questions/26654963
复制相似问题