两个不同的SelectItems返回单个选定值 [英] Two different SelectItems return single selected value

查看:87
本文介绍了两个不同的SelectItems返回单个选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个国家/地区课程:

I have a country class:

public class Country{
     private Long id;
     private String name;
}

和具有两个国家/地区"字段的人员类别

and a person class that has two Country fields

public class Person{
    private Country nationality;
    private Country nationality2;
}

现在在JSF中,我使用<f:selectItems>返回选择国籍的国家/地区列表,如下所示:

Now in JSF I use <f:selectItems> to return list of countries to select nationalities as following:

<h:form id="form1">
    <h:selectOneMenu value="#{mybean.person.nationality.id}">
         <f:selectItems value="#{mybean.countryList}" var="var" itemValue="#{var.id}"/>
    </h:selectOneMenu>

    <h:selectOneMenu value="#{mybean.person.nationality2.id}">
         <f:selectItems value="#{mybean.countryList}" var="var" itemValue="#{var.id}"/>
    </h:selectOneMenu>
    <p:commandButton actionListener="#{mybean.save}" update="sometable @form"/>
</h:form>

现在,很奇怪的问题是,当我提交表单时,无论为第一个字段选择了什么,分配给第二个字段(nationality2)的值都同时分配给了nationality和nationality2.例如,如果selected value for nationality is 1selected value for nationality2 is 2,当我提交表单both fields have the value 2时.为什么会这样?

Now the weird problem is that when I submit the form the value assigned to the second field (nationality2) is assigned to both nationality and nationality2 regardless of what has been selected for the first field. For example if the selected value for nationality is 1 and the selected value for nationality2 is 2, when I submit the form both fields have the value 2. Why is this occuring?

PS:JSF实现是Mojarra 2.1.3

PS: JSF implementation is Mojarra 2.1.3

推荐答案

您的具体问题是由于您将相同的Country引用的副本设置为选定值,然后仅操作id属性而引起的.在一个参考文献中进行的所有更改也将反映在所有其他参考文献中.

Your concrete problem is caused because you're setting copies of the same Country reference as selected value and then manipulating only the id property. All changes made in one reference get reflected in all other references as well.

例如

Country country = new Country();
person.setNationality1(country);
person.setNationality2(country);
country.setId(1); // Gets reflected in both nationalities!

您最好将整个Country实体设置为值,而不要操纵其属性.创建一个在Countryid之间转换的Converter:

You'd better set the whole Country entity as value instead of manipulating its properties. Create a Converter which converts between Country and id:

@FacesConverter(forClass=Country.class)
public class CountryConverter implements Converter {

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return (value instanceof Country) ? ((Country) value).getId() : null;
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value == null || value.isEmpty()) {
            return null;
        }

        if (!value.matches("\\d+")) {
            throw new ConverterException(new FacesMessage("Invalid country ID: " + value));
        }

        Long countryId = Long.valueOf(value);
        MyBean myBean = context.getApplication().evaluateExpressionGet(context, "#{myBean}", MyBean.class);

        for (Country country : myBean.getCountries()) {
            if (countryId.equals(country.getId())) {
                return country;
            }
        }

        throw new ConverterException(new FacesMessage("Unknown country ID: " + value));
    }

}

并按以下方式使用它:

<h:selectOneMenu value="#{mybean.person.nationality1}">
     <f:selectItems value="#{mybean.countries}" var="country" itemValue="#{country}" itemLabel="#{country.name}" />
</h:selectOneMenu>
<h:selectOneMenu value="#{mybean.person.nationality2}">
     <f:selectItems value="#{mybean.countries}" var="country" itemValue="#{country}" itemLabel="#{country.name}" />
</h:selectOneMenu>

这篇关于两个不同的SelectItems返回单个选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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