根据@flash 的回答,我在Employee 类(emp 类)中实现了转换器和equals() & hashCode()。
XHTML 的一部分:
<my:dropdown label="Employee ID" id="emp" value="#{deliveryPort.emp}" required="false">
<f:selectItem itemValue="#{null}" itemLabel="Selct One"/>
<s:selectItems value="#{deliveryPort.empList}" var="emp" label="# {emp.employeeId} #{ emp.nameFirst}"/>
<f:converter converterId="empConverter" />
</my:dropdown>
员工转换器类:
package mypackage.converters;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
@FacesConverter("empConverter")
public class EmployeeConverter implements Converter
{
public Object getAsObject(FacesContext context, UIComponent component, String value)
{
return value;
}
public String getAsString(FacesContext context, UIComponent component, Object value)
{
return value.toString();
}
}
Employee类的一部分(emp的类,据说employeeId是String):
public boolean equals(Object other)
{
return other instanceof Employee && (employeeId != null) ? employeeId.equals(((Employee) other).employeeId) : (other == this);
}
public int hashCode()
{
return employeeId != null ? this.getClass().hashCode() + employeeId.hashCode() : super.hashCode();
}
public String toString()
{
return "Employee[" + employeeId + "," + nameFirst + "]";
}
我有一个类似的问题。它对我有用。