首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >JSF,将空值注册到绑定的实例

JSF,将空值注册到绑定的实例
EN

Stack Overflow用户
提问于 2014-10-30 21:57:44
回答 1查看 142关注 0票数 1

将空值赋值给绑定的实例时遇到问题。每当在selectOneMenu中进行选择时,一切都会按预期进行(注册为cc.attrs.city的实例被设置为选定的值,如下面的示例所示),但是,当选择硬编码的空值时,我会检查我的选择是否没有转发到绑定的实例。任何帮助都会得到重视。

使用示例:

代码语言:javascript
复制
    <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>

任何选择转换器:

代码语言:javascript
复制
@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;
        }

    }
EN

回答 1

Stack Overflow用户

发布于 2014-10-30 22:36:28

尝试将null项指定为noSelectionOption,因为看起来这是您的意图:

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26654963

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档