1

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.

Misha
  • 45
  • 3
  • 9

3 Answers3

4

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I used this 1st option and i think its work fine. But still this ajax event dont want to work with entity (with normal string work fine). – Misha Jan 22 '14 at 08:10
  • You're welcome. As to the new problem, it's helpful if you elaborate the problem in developer's perspective instead of enduser's perspective. – BalusC Jan 22 '14 at 08:15
1

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

Makky
  • 17,117
  • 17
  • 63
  • 86
1

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> 
Kuba
  • 2,069
  • 23
  • 30