Converter类getAsObject将参数传递给接受对象的Service类



我对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}"在您的视图中使用它。

相关内容

  • 没有找到相关文章

最新更新