Primefaces自动完成ClassCastException

时间:2013-01-09 17:22:32

标签: spring hibernate jsf primefaces

我正在使用Spring 3,Hibernate 4和JSF 2.0,并尝试自动完成primefaces。

我在DAO类中有以下内容来从Oracle函数

获取值
@Override
    public List<EmployeeDetail> getEmployeeDetails(String employeeNumber) {

        List query = (List)entityManager.createNamedQuery("getEmp")
                .setParameter("empNumber", employeeNumber)
                .getSingleResult();

        return query;

    }

转换器类

public EmployeeNameConverter(

            List<EmployeeDetail> employeeDB, EmployeeDetailService instance,
                    String employeeNumber) {
                if (employeeDetailService == null) {
                    employeeDetailService = instance;
                }
                if (param == null) {
                    param = employeeNumber;
                }
                this.employeeDB = employeeDB;
            }

@Override
    public Object getAsObject(FacesContext arg0, UIComponent arg1,
            String submittedValue) {
        if (submittedValue.trim().equals("")) {
            return null;
        } else {
            try {
                // int number = Integer.parseInt(submittedValue);
                employeeDB = getEmployeeDetailService().getEmployeeDetails(param);
                for (EmployeeDetail emp : employeeDB) {
                    if (emp.getEmployeeNumber() == submittedValue) {
                        return emp;
                    }
                }

            } catch (NumberFormatException exception) {
                throw new ConverterException(new FacesMessage(
                        FacesMessage.SEVERITY_ERROR, "Conversion Error",
                        "Not a valid employee"));
            }
        }

在我的Managedbean中

我有完整的方法,我将employeeDetailService传递给我的转换器类。

public List<EmployeeDetail> complete(String query) {
        List<EmployeeDetail> suggestions;
        suggestions = new ArrayList<EmployeeDetail>();
        try {
            employee =  (List<EmployeeDetail>) new EmployeeNameConverter(
                    employeeList, employeeDetailService, query);

            for (EmployeeDetail p : employee) {
                if (p.getEmployeeNumber().startsWith(query))
                    suggestions.add(p);
            }
        } catch (Exception e) {
            System.out.println("exc " + e.getMessage());
            e.printStackTrace();
        }

        return suggestions;
    }

JSF代码

<p:autoComplete value="#{empMB.selectedEmployee}"
                            id="basicPojo" minQueryLength="6" 
                            completeMethod="#{empMB.complete}" var="p"
                            itemLabel="#{p.employeeNumber}" 
                            itemValue="#{p}" converter="#{p.employee}"
                            forceSelection="true" />

当我输入字符时,我会遇到异常

EmployeeNameConverter cannot be cast to java.util.List
java.lang.ClassCastException: net.test.util.EmployeeNameConverter cannot be cast to java.util.List

我该如何解决这个问题?这是正确的方法,还是有人可以提出更好的方法来实现同样的目标?

更新1

@Override
    public List<EmployeeDetail> getEmployeeDetails(String employeeNumber) {

        List query = (List)entityManager.createNamedQuery("getEmp")
                .setParameter("empNumber", employeeNumber)
                .getSingleResult();

        return query;

    }

异常

net.test.entity.EmployeeDetail cannot be cast to java.util.List
java.lang.ClassCastException: net.test.entity.EmployeeDetail cannot be cast to java.util.List
    at net.test.dao.EmployeeDetailDAOImpl.getEmployeeDetails(EmployeeDetailDAOImpl.java:36)

和例外行是

List query = (List)entityManager.createNamedQuery("getEmp")
                    .setParameter("empNumber", employeeNumber)
                    .getSingleResult();

1 个答案:

答案 0 :(得分:2)

这里有两个主要问题

  1. 您正在尝试手动实例化和管理JSF转换器实例。不要这样做。转换器是一种仅供JSF上下文使用的构造。开发人员调用new或向转换器类提供构造函数参数不是你的工作。根据{{​​3}}实施一个简单的转换器,并在<p:autoComplete/>

    上进行配置

    根据您的评论,您手动管理转换器的原因是能够访问您的DAO层。作为一种变通方法,您可以将@ManagedBean添加到转换器中,JSF将其视为托管bean 转换器。作为托管bean,您现在可以将DAO资源注入其中。这不是最佳做法,而只是一种解决方法。转换器的这种限制将在JSF2.2中删除,但是:)。

  2. 在操作转换器时,您尝试将其视为常规POJO并尝试将其转换为几种不兼容的类型

    employee = (List<EmployeeDetail>) new EmployeeNameConverter( employeeList, employeeDetailService, query); //EmployeeNameConverter !instanceof EmployeeDetail