How UISelectOne and UISelectMany components pre-select default values ​​in f: selectItems

I know how to preselect <p:selectOneMenu>

, the selected value

should have one of the objects from <f:selectItems>

, but how does this component work under the hood and can I change this behavior?

In my case, I have a duplicated object, in fact these are two objects with the same values, but they are created twice, and the selected object is <p:selectOneMenu>

different from the object from <f:selectItems>

, and it does not recognize it. I will most likely change my project so it will point to the same object, but if I cannot do this due to legacy code or such, how can I change the behavior <p:selectOneMenu>

that it will compare objects using id

eg?

I thought I was converter

responsible for it, but when it is displayed, it doesn’t only enter the method getAsObject

getAsString

, so I assume there is something else in there, but what?

thank

+3


source to share


1 answer


He uses Object#equals()

for this. You can change (fix) this behavior by implementing it appropriately 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);
}

      

Don't forget hashCode()

to satisfy equals-hashCode .

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

      



If you can't modify an existing object for some unknown reason, wrap it in your own DTO.

The converter only converts the object and its unique representation String

for use in the output of the HTML and HTTP request and therefore does not affect the preselection. This only affects the potential problem . Validation Error: Value is not valid .

See also:

+3


source







All Articles