1

I have two bean classes vehicletypes and cars from vehicletypes is a list of vehicles from which one typename is to be selected for cars class.So i am populating the list in car.xhtml by v.name its working fine using combo box.For assigning this typename to cars.name i am using listerner action as shown:

.XHTML Code

  <h:selectOneMenu>
     <f:selectItems value="#{vehicletypes.veh}" var="v"  itemLabel="#{v.name}" />
     <f:ajax event="change" listener="#{cars.combochange}" />
     <f:param name="idx" value="#{v.name}" />
  </h:selectOneMenu>

Managed bean:

public String  getcombochange(FacesContext fc)
    {

          Map<String,String> params =fc.getExternalContext().getRequestParameterMap();
          return params.get("idx");


    }
    public String combochange(){

        String type;
        FacesContext fc = FacesContext.getCurrentInstance();
        type= getcombochange(fc);
        System.out.println("\nChange occured Car type assigned"+type); 
        return "result";
    }

While printing the value of cartype on console using println it always null,Whats wrong in the code i tried to pass a defualt string as param but its also not working.


Have tried this way as well but this doesn't call the action itself;

.xhtml

<h:selectOneMenu>
<f:selectItems value="#{vehicletypes.veh}" var="v"  itemLabel="#{v.name}" />
<f:ajax event="change" listener="#{cars.combochange}" />
<f:attribute name="add" value="default" />
</h:selectOneMenu>

Bean Class

 public String combochange(ActionEvent event){
         String type = (String)event.getComponent().getAttributes().get("add");
         System.out.println("Add pool:"+type);
         return null;
    }

Dont understand why the action is not called.

Heena Jain
  • 45
  • 1
  • 1
  • 7
  • 2
    See: http://stackoverflow.com/questions/4782430/how-to-pass-additional-parameters-in-ajax-request-on-change-value-in-hselectone – Johny T Koshy Mar 12 '13 at 07:48

1 Answers1

0

You have not assigned any value in the backend bean for the value chosen. You need to modify just the to the following.

  <h:selectOneMenu value="#{cars.selectedValue}">
  <f:selectItems value="#{vehicletypes.veh}" />
  <f:ajax listener="#{cars.combochange}" />
  </h:selectOneMenu>

Where "selectedValue" is a private variable in your cars bean with getters/setters where the selected value is placed. "combochange" is the method in the cars bean, and "veh" is the list of values to be displayed.

Also, In the method "combochange" please change event to "AjaxBehaviorEvent". Since, you are calling from a ajax event, the event that fires is a ajax behaviour event.

Hope this helps :)

h-kach
  • 351
  • 2
  • 8
  • 25