primefaces selectOneMenu 在它应该工作时不起作用 [英] primefaces selectOneMenu doesn't working when it should

查看:16
本文介绍了primefaces selectOneMenu 在它应该工作时不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我因这个奇怪的问题而失去了几天,我仔细检查了所有内容,但我的 selectOneMenu 根本不起作用,我不明白为什么.

I'm losing days with this strange problem, I double checked everything but my selectOneMenu simply doesn't works and I can't understand why.

这是我的代码:

我的jsf

<p:selectOneMenu id="entityType"  
      value="#{entityBean.entity.type}" 
      style="width:240px;" 
      converter="entityTypeConverter"
      valueChangeListener="#{entityBean.entityTypeListener}"
      required="true">
      <f:selectItems value="#{entityBean.typeList}"
              var="et"
              itemLabel="#{et.name}"
              itemValue="#{et}" />
</p:selectOneMenu>

我的转换器:

    @FacesConverter("entityTypeConverter")
    public class EntityTypeConverter implements Converter {
        public Object getAsObject(FacesContext context, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            Long id = Long.parseLong(value);

            return EntityType.findEntityType(id);
        }

        public String getAsString(FacesContext context, UIComponent component, Object value) {

            return value instanceof EntityType ? ((EntityType) value).getId().toString() : "";
        }
    }

它在我创建时按预期工作(它传递所选值),但是当我尝试编辑实体时,所选类型实际上从未被选中.我尝试使用 primefaces 3.1.1 和 3.2,但在查看/编辑模式下无法获得所选值.

It works as expected when I'm creating (it passes the selected value), but when I try to edit the entity the selected type actually never gets selected. I tried with primefaces 3.1.1 and 3.2 but I can't get the selected value when in view/edit mode.

我做错了什么?

提前致谢!

推荐答案

如果 EntityType 类的 equals() 方法丢失或损坏,就会发生这种情况.鉴于您的 EntityType 类中有一个 id 属性,它似乎足以唯一地标识实例,以下最小实现应该为您完成:

That can happen if the equals() method of your EntityType class is missing or broken. Given the fact that you've an id property in your EntityType class which seems to identify the instance uniquely enough, the following minimal implementation should do it for you:

@Override
public boolean equals(Object other) {
    return (other instanceof EntityType) && (id != null)
        ? id.equals(((EntityType) other).id)
        : (other == this);
}

@Override
public int hashCode() {
    return (id != null)
        ? (this.getClass().hashCode() + id.hashCode())
        : super.hashCode();
}

hashCode() 根据 equals() 合同.

这篇关于primefaces selectOneMenu 在它应该工作时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆