更新ajax后,jsf返回一个对象,而不是我的对象

时间:2014-08-12 14:25:33

标签: ajax jsf

我的表单有问题,我解释一下: 我希望当用户选择联系人列表更新的客户端时,新的联系人列表对应于所选择的客户端。 我的自动完成工作正常。 我的表格:

<p:outputLabel for="clientDelivery"
                    value="#{msg['material-newDelivery.clientDelivery']}" />
                <p:autoComplete id="clientDelivery" dropdown="true"
                    value="#{deliveryNew.client}"
                    completeMethod="#{deliveryNew.complete}"
                    converter="clientConverter" var="c" itemLabel="#{c.toString()}"
                    itemValue="#{c}" forceSelection="true" required="true">
                    <p:ajax update="contactDelivery" execute="@this" listener="#{deliveryNew.selectOneMenuListener}"/>
                </p:autoComplete>

                <p:outputLabel for="contactDelivery"
                    value="#{msg['material-newDelivery.contactDelivery']}" />
                <h:selectOneMenu id="contactDelivery"
                    value="#{deliveryNew.contact}" required="true"
                    converter="contactConverter" var="c" itemLabel="#{c.lastName}" itemValue="#{c}" >
                    <f:selectItems value="#{deliveryNew.contacts}" var="c"
                        itemLabel="#{c.toString()}" itemValue="#{c}" />
                </h:selectOneMenu>

我的托管bean:

@PostConstruct
public void init(){
    contacts = contactDao.findAll();
    contacts.sort(new ContactComparator());
}

public void selectOneMenuListener() {
    this.contacts.clear();
    this.contacts = contactDao.getByClient(client.getId());   
}

public List<Client> complete(String query){
    return clientDao.getByClientNameAll(query);    
}

我的联系人转换器:

    @FacesConverter(value = "contactConverter", forClass=Contact.class)
public class ContactConverter implements Converter {

    @EJB
    private ContactDao contactDao;

    @Override
    public Object getAsObject(FacesContext ctx, UIComponent component,
            String value) {
        return contactDao.getByContactName(value);
    }

    @Override
    public String getAsString(FacesContext fc, UIComponent uic, Object value) {
        if (value == null || value.equals("")) { 
            return "";  
        } else { 
            return ((Contact) value).getLastName();
        }
    } 

我的客户端转换器工作正常。 我的ContactDao:

public List<Contact> getByClient(Long idClient){
    List<Contact> contacts = new ArrayList<Contact>();
    Query query = em.createNativeQuery("SELECT * FROM Contact WHERE client_id = ?client_id");
    query.setParameter(PARAM_IDCLIENT, idClient);
    try{
        contacts = (List<Contact>) query.getResultList();
    }catch(NoResultException e){
        contacts.clear();
        Contact emptyList = new Contact();
        emptyList.setLastName("Aucune donnée");
        contacts.add(emptyList);
    }   
    return contacts;
}

public Contact getByContactName(String lastName){
    Query query = em.createQuery("SELECT e FROM Contact e WHERE e.lastName = :lastName");
    query.setParameter(PARAM_LASTNAME, lastName);
    return (Contact) query.getSingleResult();
}

因此,当我使用contactConverter时,我收到以下错误消息:

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to ac.imagine.beans.entities.Contact
at ac.imagine.beans.managed.ContactConverter.getAsString(ContactConverter.java:50)
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getFormattedValue(HtmlBasicRenderer.java:521)
at com.sun.faces.renderkit.html_basic.MenuRenderer.renderOption(MenuRenderer.java:534)
at com.sun.faces.renderkit.html_basic.MenuRenderer.renderOptions(MenuRenderer.java:794)
at com.sun.faces.renderkit.html_basic.MenuRenderer.renderSelect(MenuRenderer.java:847)
at com.sun.faces.renderkit.html_basic.MenuRenderer.encodeEnd(MenuRenderer.java:297)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:924)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)

当我不使用contactconverter时,我的selectOneMenu显示了对象的引用,如[Ljava.lang.Object;@20c5997e

2 个答案:

答案 0 :(得分:0)

我已经解决了我的问题, 我创建了createNativeQuery而不是createQuery,他无法返回一个对象Contact 错误在这里:

public List<Contact> getByClient(Client client){
    List<Contact> contacts = new ArrayList<Contact>();
    Query query = em.createQuery("SELECT c FROM Contact c WHERE c.client = :client_id");
    query.setParameter(PARAM_IDCLIENT, client);
    try{
        contacts = (List<Contact>) query.getResultList();
    }catch(NoResultException e){
        contacts = null;
        System.out.println("erruer lors de la recup des contacts");
    }
    return contacts;
}

答案 1 :(得分:-1)

您的问题出在getAsString方法中,因此请将其替换为:

@Override
public String getAsString(FacesContext fc, UIComponent uic, Object value) {
    if (value == null || value.equals("")) { 
        return "";  
    } else { 
        return ((Contact) value).getLastName();
    }
}

然后看看结果:

@Override
public String getAsString(FacesContext fc, UIComponent uic, Object value) {

        return ((Contact) value).getLastName();

}