UISelectOne 和 UISelectMany 组件如何在 f:selectItems 中预选默认值 [英] How do UISelectOne and UISelectMany components preselect defaults in f:selectItems

查看:13
本文介绍了UISelectOne 和 UISelectMany 组件如何在 f:selectItems 中预选默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何预选,在选定的value 中应该是 中的对象之一code>,但是这个组件是如何在幕后工作的,我可以改变这种行为吗?

I know how to preselect <p:selectOneMenu>, in selected value should be one of the objects from <f:selectItems>, but how does this component work under the hood and can I change this behavior?

就我而言,我有一个重复的对象,实际上这是两个具有相同值但创建两次的对象,并且 <p:selectOneMenu> 中的选定对象与 < 中的对象不同;f:selectItems> 并且它无法识别它.很可能我会改变我的设计,所以它会指向同一个对象,但如果由于遗留代码或其他原因我不能这样做,我该如何改变 <p:selectOneMenu> 例如,它将通过 id 比较对象?

In my case I've a duplicate object, actually this is two objects with the same values but created twice and selected object in <p:selectOneMenu> differs from object from <f:selectItems> and it doens't recognize it. Most likely I will change my design so, it will point to same object but in case I can't do it due to legacy code or etc, how can I change the behavior of <p:selectOneMenu> that it will compare objects by id for example?

我认为 converter 对它负责,但是当它呈现时它不会进入 getAsObject 方法只有 getAsString,所以我想有一些不同的东西,但什么?

I'd thought that converter responsible for it, but when it rendered it doesn't enter on getAsObject method only getAsString, so I guess that there's something different, but what?

谢谢

推荐答案

它使用 Object#equals() 为此.您可以通过在您的实体上相应地实施它来更改(修复)此行为.

It uses Object#equals() for that. You can change (fix) this behavior by implementing it accordingly on your entity.

private Long id;

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

不要忘记 hashCode() 满足equals-hashCode 合约.

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

如果由于某些不清楚的原因您无法更改现有实体,请将其包装在您自己的 DTO 中.

If you can't change the existing entity for some unclear reason, wrap it in your own DTO.

转换器仅在实体与其唯一的 String 表示之间进行转换,以用于 HTML 输出和 HTTP 请求参数,因此对预选没有影响.它只影响潜在的验证错误:值无效问题.

The converter only converts between the entity and its unique String representation for usage in HTML output and HTTP request parameters and has therefore no influence on preselection. It has only influence on potential Validation Error: Value is not valid trouble.

这篇关于UISelectOne 和 UISelectMany 组件如何在 f:selectItems 中预选默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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