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

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

问题描述

我知道如何预选<p:selectOneMenu>,在选定的value中应该是<f:selectItems>中的对象之一,但是该组件如何在幕后工作,我可以更改此行为吗?

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是它的责任,但是当它呈现时,它不会仅在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?

谢谢

推荐答案

它使用

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() 以满足如果由于某些不清楚的原因而无法更改现有实体,请将其包装在您自己的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天全站免登陆