selectOneMenu在提交后始终显示列表中的最后一项作为选定项 [英] selectOneMenu shows after submit always the last item in the list as selected item

查看:115
本文介绍了selectOneMenu在提交后始终显示列表中的最后一项作为选定项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

信息


  • JSF 2.0

  • Primefaces 3.4

  • 两个对象都是People对象

我有一个简单的下拉菜单,该菜单包含在一个表单中,该表单在提交表单时提交选择(AJAX调用):

I have a simple drop down menu that is contained within a form that submits the selection on form submit (AJAX call):

<h:form>

....

                    <p:selectOneMenu converter="personconverter"
                        value="#{searchperson.viewPerson.relatedTo}" filter="true"
                        filterMatchMode="startsWith">
                        <f:selectItems value="#{searchperson.people}" var="person"
                            itemLabel="#{person.fullName}" itemValue="#{person}" />
                    </p:selectOneMenu>
....
                    <p:commandButton value="Save"
                        actionListener="#{searchperson.updatePerson}" />
</h:form>

当我向服务器提交请求时,与selectOneMenu绑定的对象正确传递了

When I submit the request to the server, the object tied to the selectOneMenu is passed correctly AND I am able to update my backend with this change.

重新渲染页面时,p:selectOneMenu中的值:

When the page is re-rendered, the value in p:selectOneMenu:

(value="#{searchperson.viewPerson.relatedTo}")

不呈现刚刚提交的新更改。它会呈现人员列表中的最后一个Person对象。

Does not render the new change that was JUST submitted. It renders the last Person object in the people list.

以下是其他部分:

转换器:

@FacesConverter("personconverter")
public class PersonConverter implements Converter {

@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) {

    People pApi = new People();
    Person per = new Person();

    try {
        per = pApi.getPerson(Long.parseLong(value));
    }
    catch(Exception e) {
        e.printStackTrace();
    }
    return per;
}

@Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {

    return String.valueOf(((Person) arg2).getId());
}

}

对象类

@Override
public boolean equals(Object object) {
    return true; //just to pass through temporarily
}

@Override
public int hashCode() {
    return 0;
}

face-config.xml

face-config.xml

<converter>
    <converter-for-class>com.obj.Person</converter-for-class>
    <converter-class>com.converter.PersonConverter</converter-class>
</converter


推荐答案

您的实现 equals() hashCode()被严重破坏。这样, Person 的每个实例被认为彼此相等,因此,JSF将无法基于该列表标识正确的选定项目。可用项目。您必须至少根据其合同

Your implementations of equals() and hashCode() are severely broken. This way every instance of Person is considered equal with each other and hence JSF won't be able to identify the right selected item based on the list of available items. You must implement them at least according their contracts.

鉴于其具有 id 属性,请相应地进行修复表示唯一标识符:

Fix it accordingly, given that it has an id property representing the unique identifier:

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

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

这篇关于selectOneMenu在提交后始终显示列表中的最后一项作为选定项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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