Search code examples
richfaces

Rich Faces 4.1.0 picklist


My problem is with Rich Faces 4.1.0 picklist. I have the item working in that I can populate the left and right sides. I can't get the data from the right hand side (i.e. employeeBean.target), I don't know why. Could someone please look at this code and advise me where I may have gone wrong.

View:

<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>

Controller:

    @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));
        }
    }
}

Model:

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();
}

Solution

  • My trouble lay with my converter. The getAsObject as it stands here takes in a string and passes out a string. The method will compile but not do anything useful. I fixed this by getting getAsObject to return my model bean object and not just a string.