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

时间:2013-02-22 09:49:01

标签: java jsf jsf-2 converter

我对转换器类getAsObject方法

有疑问

我有一项名为EmployeeService employeeService

的服务
public List<Employees> getEmployees(Employees employees);

如果我想从Converter类的getAsObject调用上面的方法作为参数

public Object getAsObject(FacesContext facesContext, UIComponent component,
            String value)
    employeeService.getEmployees(<?>)

如何从employeesgetEmployees对象传递给getAsObject方法?

非常感谢任何帮助。

更新1

@Entity
public class Employees implements Serializable {

private String employeeNumber;
private String employeeName;

1 个答案:

答案 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}"在您的观看中使用它。