我对转换器类getAsObject
方法
我有一项名为EmployeeService employeeService
public List<Employees> getEmployees(Employees employees);
如果我想从Converter类的getAsObject调用上面的方法作为参数
public Object getAsObject(FacesContext facesContext, UIComponent component,
String value)
employeeService.getEmployees(<?>)
如何从employees
将getEmployees
对象传递给getAsObject
方法?
非常感谢任何帮助。
更新1
@Entity
public class Employees implements Serializable {
private String employeeNumber;
private String employeeName;
答案 0 :(得分:3)
使用以下转换器,只需将数字(根据您的代码为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}"
在您的观看中使用它。