简体   繁体   English

Primefaces自动完成ClassCastException

[英]Primefaces autocomplete ClassCastException

I am using Spring 3, Hibernate 4 and JSF 2.0 and am trying to do primefaces autocomplete. 我正在使用Spring 3,Hibernate 4和JSF 2.0,我正在尝试使用primefaces自动完成。

I have the following in DAO class to get values from Oracle function 我在DAO类中有以下内容来从Oracle函数中获取值

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

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

        return query;

    }

Converter class 转换器类

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"));
            }
        }

In my Managedbean 在我的Managedbean中

I have complete method where I am passing employeeDetailService to my converter class. 我有完整的方法,我将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 Code 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" />

When I type in characters I am getting exception 当我输入字符时,我会遇到异常

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

How can I resolve this? 我该如何解决这个问题? Is this the correct approach or could someone kindly suggest better approach in achieving the same? 这是正确的方法,还是有人可以提出更好的方法来实现同样的目标?

Update 1 更新1

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

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

        return query;

    }

Exception 例外

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)

and exception line is 和例外行是

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

There are two main issues apparent here 这里有两个主要问题

  1. You're attempting to instantiate and manage a JSF converter instance by hand. 您正在尝试手动实例化和管理JSF转换器实例。 Don't do this. 不要这样做。 The converter is a construct designed for use by the JSF context only. 转换器是一种仅供JSF上下文使用的构造。 It's not the job of you the developer to call new or supply constructor arguments to the converter class. 开发人员不应该为转换器类调用new或者提供构造函数参数。 Implement a simple converter per this example and configure on the <p:autoComplete/> 根据此示例实现一个简单的转换器,并在<p:autoComplete/>

    From your comments, the reason you've taken to managing the converter by hand is to be able to access your DAO layer. 根据您的评论,您手动管理转换器的原因是能够访问您的DAO层。 As a workaround, you can add @ManagedBean to your converter and JSF will treat is as a managed bean and a converter. 作为一种解决方法,您可以将@ManagedBean添加到转换器中,JSF将其视为托管bean 转换器。 Being a managed bean, you'll now be able to inject your DAO resource into it. 作为托管bean,您现在可以将DAO资源注入其中。 This is not best practice, but only a workaround. 这不是最佳做法,而只是一种解决方法。 This limitation on converters will be removed in JSF2.2 though :). 转换器的这种限制将在JSF2.2中删除:)。

  2. While manipulating the converter, you're attempting to treat it like a regular POJO and trying to cast it into several incompatible types as a result 在操作转换器时,您尝试将其视为常规POJO并尝试将其转换为几种不兼容的类型,因此

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM