我对Converter类getAsObject
方法有疑问
我有一个叫EmployeeService employeeService
的服务
public List<Employees> getEmployees(Employees employees);
如果我想从Converter类的getAsObject调用上面的方法,它作为参数
public Object getAsObject(FacesContext facesContext, UIComponent component,
String value)
employeeService.getEmployees(<?>)
如何将employees
对象从getAsObject
传递到getEmployees
方法?
非常感谢您的帮助。
更新1
@Entity
public class Employees implements Serializable {
private String employeeNumber;
private String employeeName;
使用以下转换器,它只需将数字(根据您的代码,它是String
)转换为Employee
对象:
@Named("employeeConverter")
public class EmployeeConverter implements Converter {
@Inject
private EmployeeService employeeService;
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if(value == null || value.equals("")) {
return null;
}
Employee employee = employeeService.getEmployeeByNumber(value);//or if it is not a String, replace value with Integer.parseInt(value)
if(employee == null) {
throw new ConverterException(new FacesMessage("Employee with number: " + value + " not found."));
}
return employee;
}
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (!(value instanceof Employee) || (value == null)) {
return null;
}
return ((Employee)value).getEmployeeNumber();
}
}
并通过converter="#{employeeConverter}"
在您的视图中使用它。