1

Possible Duplicate:
Primefaces selectOneMenu listener not called with Objects other than Strings

I am creating List of SelectItem as:

List<SelectItem> activityGlobalTypes = new ArrayList<SelectItem>();

for (ActivityTypeXMLModel activityTypeXMLModel : filteredActivityTypeXMLModels) {
    activityGlobalTypes.add(new SelectItem(activityTypeXMLModel, activityTypeXMLModel.getParent()));
}

This is the dropdown menu:

<p:selectOneMenu id="activityGlobalMenu" value="#{adminController.activityDTO.activityParentName}" required="true">
    <f:selectItem itemLabel="Select One" itemValue="" />  
    <f:selectItems value="#{adminController.activityGlobalTypes}" />
    <p:ajax listener="#{adminController.updateDependentActivity}" />                        
</p:selectOneMenu>

Now from the method updateDependentActivity

public void updateDependentActivity(AjaxBehaviorEvent event) {
    SelectOneMenu menu = (SelectOneMenu) event.getComponent();
    System.out.println(menu.getValue());//it prints com.edfx.adb.xml.model.ActivityTypeXMLModel@7b6fe3c4

    if(menu.getValue() instanceof ActivityTypeXMLModel) {
        System.out.println("value is ActivityTypeXMLModel"); // I was expecting this one
    } else if(menu.getValue() instanceof String) {
        System.out.println("value is String"); //this line is executing 
    } else {
        System.out.println("unknown");
    }   
}

I have checked the source code of SelectItem and it is storing value in object form. Any pointer would be very helpful.

Community
  • 1
  • 1
Tapas Bose
  • 28,796
  • 74
  • 215
  • 331
  • 1
    To the point, you need a converter or an alternate approach. See also `[selectonemenu]` tag wiki page for some hints: http://stackoverflow.com/tags/selectonemenu/info – BalusC Jan 16 '13 at 12:19
  • 1
    Or, if you happen to use the JSF utility library OmniFaces already, then all you need to do is to add `converter="omnifaces.SelectItemsConverter"` attribute to the component. – BalusC Jan 16 '13 at 12:37
  • @BalusC I have used `omnifaces.SelectItemsConverter` after your suggestion and also I have changed the datatype of `adminController.activityDTO.activityParentName` to String to `ActivityTypeXMLModel`, then it works. Also extending `org.omnifaces.converter.SelectItemsConverter` it works. Is there any way to let the datatype of `adminController.activityDTO.activityParentName` as `String` and achieve what I want? – Tapas Bose Jan 16 '13 at 13:01

2 Answers2

4

To the point, your problem is caused because non-standard Java objects (i.e. no String, Number or Boolean for which JSF has builtin converters) are by default converted to String by a simple Object#toString() call. So complex objects end up as their toString() representation in the generated HTML <option value>. Exactly this value get submitted back to the server.

You can solve this in different ways:

  1. Provide a custom Converter which converts between ActivityTypeXMLModel and String. This is answered in among others the following question: Primefaces selectOneMenu listener not called with Objects other than Strings

  2. If you're using the JSF utility library OmniFaces, then you can also use the provided converters omnifaces.SelectItemsConverter or omnifaces.SelectItemsIndexConverter to perform the generic conversion based on the <f:selectItem(s)> value without the need to write a custom converter.

  3. Do not supply a complex Java object type as item value, but instead supply a standard Java object type, such as String. In your case, you seem to want to use the activityParentName property of the ActivityTypeXMLModel object. For easy traversion using the since JSF2 introduced var attribute of <f:selectItems>, you only need to change the value to be a List<ActivityTypeXMLModel> instead of List<SelectItem>.

    private List<ActivityTypeXMLModel> activityGlobalTypes;
    

    with

    <f:selectItems value="#{adminController.activityGlobalTypes}" var="type"
        itemValue="#{type.activityParentName}" itemLabel="#{type.parent}" />
    

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

Is adminController.activityDTO.activityParentName a selectItem or a String? Perhaps, it should be a selectItem in this case.