jsf - Primefaces selectOneMenu not displaying initial value -


i set selectonemenu pojos , converter, in p:dialog, see sources below. does work, except initially, when first displayed in not-dropped-down state, first choice selected, not 1 corresponding bean value. if save state of selectonemenu without interacting @ all, selected first choice saved , real value overwritten, if select differenct choice, saved properly. bean value selectonemenu bound can't null.

i debugged converter, , turned out, when backing data loaded , dialog refreshed , displayed, of choices go through converter's getasstring(), plus choice real bean value again. still, first choice gets selected , displayed in selectonemenu. when dialog's form commited, selected choice goes through converter's getasobject(), regardless whether wrongly selected initial value or manually selected one.

please advise might problem.

the xhtml of button invokes dialog, in different form:

<p:commandbutton id="toolbareditbutton"              value="edit selected" update=":editmediaform"             disabled="#{!contentmanager.mediaselected}"             actionlistener="#{contentmanager.editselectedmedia}"             onclick="pf('editmediawidget').show()" /> 

the xhtml of dialog:

    <p:dialog id="editmediadialog" widgetvar="editmediawidget"         modal="true" resizable="false" >         <h:form id="editmediaform" >             <p:panelgrid rendered="#{contentmanager.ismediaselected()}" columns="2" >                 ... <!-- other properties of selected element -->                 <p:outputlabel value="media type" />                 <p:selectonemenu value="#{contentmanager.selectedmedia.mediatype}"                                  converter="#{mediatypeconverter}">                     <f:selectitems value="#{mediatypeconverter.allmediatypes}"                         var="mt" itemlabel="#{mt.name}" itemvalue="#{mt}" />                     <p:ajax listener="#{contentmanager.onmediatypechanged()}" />                 </p:selectonemenu>             </p:panelgrid>         </h:form>     </p:dialog> 

the converter:

@override public object getasobject(facescontext arg0, uicomponent arg1, string stringid) {     long id = long.valueof(stringid);     (mediatype mt : mediatypes) {         if (mt.getpkid().equals(id)) {             return mt;         }     }     return null; }  @override public string getasstring(facescontext arg0, uicomponent arg1, object mtobj) {     mediatype mt = (mediatype) mtobj;     return mt.getpkid().tostring(); }  public list<mediatype> getallmediatypes() {     return mediatypes; } 

edit: backing bean

@sessionscoped // javax.enterprise.context.sessionscoped @named("contentmanager") // javax.inject.named public class contentmanager implements serializable {     ...     private list<media> medialist;     private media selectedmedia = null;     ...     public boolean ismediaselected() {         if (selectedmedia == null) return false;         return true;     }     ...     public void saveselectedmedia() {         mydao.savemedia(selectedmedia);     }      public void editselectedmedia() {         // debugging, removed     } } 

the dialog brought , form updated edit button available after element selected datatable (selectedmedia). update seem work, since other properties of selected element updated , displayed in dialog, bean value behind selectonemenu should ok.

update: of course examined generated html. <select> seems ok me, contains proper values converted converter. (the selection still wrong)

<select id="form:blah_input" name="form:blah_input" tabindex="-1">     <option value="1" selected="selected">1 - half_horizontal</option>     <option value="2">2 - half_vertical</option>     <!-- etc --> </select> 

the objects displayed in selectonemenu have have proper equals() method, not default object#equals true if same object. explains why initial displayed value first: bean value never matched of possible values, selectonemenu component displayed first one.

so mistake not in jsf or backing bean code, in displayed domain objects' (mediatype) code. adding equals() method there solved problem.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -