转换器类抛出异常java.lang.IllegalArgumentException

时间:2013-02-21 07:18:01

标签: java jsf jsf-2 primefaces

我在我的JSF页面中有p:selectOneMenu,当我运行我的JSF页面时,我从Converter类中得到以下异常。

java.lang.IllegalArgumentException: object 5634 is of type java.lang.String; 
expected type: test.entity.Employee

如何解决此问题?

我在实体类

中有以下内容
@Entity
public class Employee implements Serializable {

private String employeeNumber;
private String employeeName;

/* getters and setters */

EmployeeConverter类

@Component("employeeConverter")
@FacesConverter(forClass = Employee.class)

@Inject
EmployeeService employeeService;

@Override
public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {

    if (value == null || value.length() == 0) {
        return null;
    }
    EmployeeService employeeService= (EmployeeService ) facesContext.getApplication().getELResolver().
            getValue(facesContext.getELContext(), null, "employeeService");
    return employeeService.getEmployees();
}

@Override   
public String getAsString(FacesContext facesContext, UIComponent component, Object object) {

            if (object == null) {
                return null;
            }
            if (object instanceof Employee) {
                Employee emp = (Employee) object;
                String val = emp.getEmployeeNumber();
                return val;
            } else {
throw new IllegalArgumentException("object " + 
object + " is of type " + object.getClass().getName() + "; expected type: " +
Employee.class.getName());
            }
}

和jsf页面

 <p:selectOneMenu value="#{mb.employee}" converter="#{employeeConverter}">
 <f:selectItems value="#{cmb.employeeList}" var="emp"
 itemLabel="#{emp.employeeName}" itemValue="#{emp.employeeNumber}"/>
 <p:ajax  listener="#{mb.handleChange}"  process="@this"/>
 </p:selectOneMenu>

更新1

修改过的转换器方法getAsObject

private List<Employee> employee;

@Override
    public Object getAsObject(FacesContext facesContext, UIComponent component,
            String value) {
        System.out.println("reached in converter");
        try {
            if (value == null || value.length() == 0) {
                return null;
            } else {
                employee = getEmployeeService().getEmployees();
                for (Employee emp : employee) {
                    if (emp.getEmployeeNUmber() == value) {
                        return emp;
                    }
                }
            }

        } catch (Exception e) {
            System.out.println("exception from getAsObject  " + e.getMessage());            
        }
        return null;
    }

更新2

ManagedBean

@PostConstruct
    public void loadEmployees(){

        try {           
            List<Employee> emp = getEmployeeService().getEmployees();           
            employeeList = emp; 

        }
        catch(Exception e){
            e.printStackTrace();
            System.out.println("exception from loadEmployees "+e.getMessage());
        }

    }

2 个答案:

答案 0 :(得分:3)

不要将字符串分配给项目值,您需要分配转换后的对象(Employee)本身。

更改为itemValue="#{emp}"

请参阅BalusC对question的回答。

答案 1 :(得分:2)

getAsObject方法中,您对达到的值无效。在这里,您将收到#{emp.employeeNumber},因此您必须将其转换为Employee(可能您必须使用服务定位器实现按ID加载Employee的方法)并返回对象本身。

您还使用EL(#{employeeConverter})引用转换器,您必须以直接方式执行此操作。最后,元素列表必须由SelectItem组成,以使f:selectItems标记起作用。