1

我的问题是 Rich Faces 4.1.0 选项列表。我有一个可以填充左右两侧的项目。我无法从右侧(即employeeBean.target)获取数据,我不知道为什么。有人可以看看这段代码并告诉我哪里可能出错了。

看法:

<rich:panel style="width:560px;">
            <h:form>
                <rich:pickList value="#{employeeBean.target}" var="emp" id="picklist"
                               sourceCaption="Available" targetCaption="Selected"
                               orderable="false"  converter="employeeConverter">
                    <f:selectItems value="#{employeeBean.source}" var="employee" itemValue="#{employee}" />
                    <f:converter converterId="employeeConverter" />
                </rich:pickList>
            </h:form>
            jobcardBean bean = #{employeeBean.target}
        </rich:panel>

控制器:

    @ManagedBean
@ViewScoped
public class EmployeeBean implements Serializable {

    private static final long serialVersionUID = 1L;
    List<Employee> source;
    List<Employee> target;

    public EmployeeBean() {
    }

    public List<Employee> getSource() {
        return new DatabaseBean().getAllEmployees();
    }

    public void setSource(List<Employee> source) {
        this.source = source;
    }

    public List<Employee> getTarget() {
        return target;
    }

    public void setTarget(List<Employee> target) {
        this.target = target;
    }

/ * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * **** /

    @FacesConverter(value = "employeeConverter")
public class EmployeeConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) {
        if (submittedValue.trim().equals("")) {
            return null;
        } else {
            return submittedValue.toString();
        }
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value == null || value.equals("")) {
            return "";
        } else {
            return String.valueOf(((Employee) value));
        }
    }
}

模型:

public String toString() {
    return getName() + " " + getSurname();
}

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (!(obj instanceof Employee)) {
        return false;
    }

    return ((Employee) obj).getName() == this.name && ((Employee) obj).getSurname() == this.surname;
}

@Override
public int hashCode() {
    int hash = 1;
    return hash * 31 + name.hashCode();
}
4

1 回答 1

0

我的麻烦在于我的转换器。这里的 getAsObject 接受一个字符串并传递一个字符串。该方法将编译但不会做任何有用的事情。我通过让 getAsObject 返回我的模型 bean 对象而不仅仅是一个字符串来解决这个问题。

于 2012-06-17T18:44:37.913 回答