"jsf converter issue in selectonemenu " Code Answer

2

the getasstring() should convert the object (which is in your case of type tdpublictype) to a string which uniquely identifies the instance, e.g. some id, so that it can be inlined in html code and passed around as http request parameters. the getasobject() should convert exactly that unique string representation back to the concrete object instance, so that the submitted http request parameter can be converted back to the original object instance.

basically (trivial prechecks and exception handling omitted):

@override
public string getasstring(facescontext context, uicomponent component, object modelvalue) throws converterexception {
    // convert object to unique string representation for display.
    return string.valueof(((tdpublictype) modelvalue).getid());
}

@override
public object getasobject(facescontext context, uicomponent component, string submittedvalue) throws converterexception {
    // convert submitted unique string representation back to object.
    return tdpublictypeservice.find(long.valueof(submittedvalue));
}

update: you've another problem, you're specifying the value property of tdpublictype class as the item value instead of the tdpublictype instance itself. this way the converter will retrieve the value property instead of the tdpublictype instance in the getasstring(). fix it accordingly:

<f:selectitems value="#{publicobean.lstpublictypes}" var="pt" 
    itemlabel="#{pt.label}" itemvalue="#{pt}"/>
By Loint on January 28 2022

Answers related to “jsf converter issue in selectonemenu ”

Only authorized users can answer the Search term. Please sign in first, or register a free account.