Misha
Misha

Reputation: 55

issue with ajax in primefaces "selectOneMenu"

index.xhtml

        <p:selectOneMenu id="d2" value="#{mainManageBean.areaSelected}"  >
            <f:selectItem itemValue="" itemLabel="Select one" />
            <f:selectItems value="#{mainManageBean.areaList}" var="area"
                           itemValue="#{area.id}" itemLabel="#{area.name}"/> 
        <p:ajax event="valueChange" listener="#{mainManageBean.changeAreaSelect()}" update="hi"  /> 
        </p:selectOneMenu>

When i have value set like this "mainManageBean.areaSelected" where areaSelected is entity from database

private Area areaSelected; 

the ajax event dont work, but when i change it to something like this "mainManageBean.s1menu" where this "s1menu" is just a normal String ajax event work fine.

What is the reason of that and how to fix it?

Edit

this is my buged converter:

@FacesConverter
public class areaConverter implements Converter{


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

    // here i have problem value is id of entity and have no idea how to get this entity form this id

    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

// works fine value = Area entity
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
   if (value == null || value.equals("")) {  
        return "";  
    } else {  
        return String.valueOf(((Area) value).getId());  
    } 
}    
}

Have problem with getAsObject, i have this value as id but cant get entity from it. Normaly i would use AreaFacade.find(id) but i cant use there @EJB to get it.

Upvotes: 1

Views: 3503

Answers (3)

BalusC
BalusC

Reputation: 1108632

First of all, your <f:selectItem itemValue> is wrong. It should represent exactly the same type as <p:selectOneMenu value>, which is thus Area. Replace itemValue="#{area.id}" by itemValue="#{area}". You indeed need a Converter for this.

As to your problem with the converter,

Have problem with getAsObject, i have this value as id but cant get entity from it. Normaly i would use AreaFacade.find(id) but i cant use there @EJB to get it.

you have 2 options:

  1. Make it a @ManagedBean @RequestScoped instead of @FacesConverter and reference it as converter="#{areaConverter}" instead of converter="areaConverter".

  2. Install OmniFaces >= 1.6. It adds full transparent support for @EJB inside @FacesConverter without any additional configuration or annotations.

If you go the OmniFaces path anyway, then you could also just throw away your custom converter altogether and go for its builtin SelectItems(Index)Converter without the need to create any custom converter for itemValue="#{area}".

<p:selectOneMenu ... converter="omnifaces.SelectItemsConverter">

See also:

Upvotes: 4

Makky
Makky

Reputation: 17463

As per comment from Kuba

<f:selectItems value="#{mainManageBean.areaList}" var="area"
                           itemValue="#{area.id}" itemLabel="#{area.name}"/> 

change itemValue as

<f:selectItems value="#{mainManageBean.areaList}" var="area"
                           itemValue="#{area}" itemLabel="#{area.name}"/> 

Update:

The other thing it could be the converter. My suggestion is to use the SelectItemsConverter from Onmnifaces.

Omnifaces select item converter

Upvotes: 1

Kuba
Kuba

Reputation: 2089

You are trying to set an Entity with value of ID, I assume Integer or String? The reason why ajax does not fire is because event="valueChange" does not occur. If you tried to submit this form without ajax you would get a sweet ClassCastException. As I mentioned in my comment and Makky in his answer, change itemValue to:

<p:selectOneMenu id="d2" value="#{mainManageBean.areaSelected}"  >
        <f:selectItem itemValue="#{null}" itemLabel="Select one" />
        <f:selectItems value="#{mainManageBean.areaList}" var="area"
                       itemValue="#{area}" itemLabel="#{area.name}"/> 
    <p:ajax listener="#{mainManageBean.changeAreaSelect()}" update="hi" process="@this /> 
 </p:selectOneMenu> 

Upvotes: 1

Related Questions