-1

getAsObject我对 Converter 类方法有疑问

我有一个服务叫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;
4

1 回答 1

3

使用以下转换器,它只是将数字(String根据您的代码是 a )转换为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}"

于 2013-02-22T10:27:05.953 回答