1

我的 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());
        }

    }
4

2 回答 2

3

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

改成itemValue="#{emp}"

请参阅 BalusC 对此问题的回答。

于 2013-02-21T07:27:11.823 回答
2

在您的getAsObject方法中,您没有对达到的价值做任何事情。在这里您将收到您的#{emp.employeeNumber},因此您必须将其转换为Employee(可能您必须实现一个Employee使用服务定位器加载按 id 的方法)并返回对象本身。

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

于 2013-02-21T07:31:48.810 回答