Note that there are some explanatory texts on larger screens.

plurals
  1. POh:selectOneMenu not populating a 'selected' item
    primarykey
    data
    text
    <p>I'm having trouble with an h:selectOneMenu not having a selected item when there is already something set on the backing bean. I am using seam and have specified a customer converter. When working on my 'creation' page, everything works fine, something from the menu can be selected, and when the page is submitted, the correct value is assigned and persisted to the database as well.<br> However when I work on my 'edit' page the menu's default selection is not the current selection. I have gone through and confirmed that something is definitely set etc.</p> <p>My selectOneMenu looks like this:</p> <pre><code>&lt;h:selectOneMenu id="selVariable" value="#{customer.variableLookup}" converter="#{variableLookupConverter}"&gt; &lt;s:selectItems var="source" value="#{customerReferenceHelper.variableLookups()}" label="#{source.name}" /&gt; &lt;/h:selectOneMenu&gt; </code></pre> <p>And the converter is below. It very simple and just turns the id from string to int and back etc:</p> <pre><code>@Name( "variableLookupConverter" ) public class VariableLookupConverter implements Serializable, Converter { @In private CustomerReferenceHelper customerReferenceHelper; @Override public Object getAsObject( FacesContext arg0, UIComponent arg1, String arg2 ) { VariableLookup variable= null; try { if ( "org.jboss.seam.ui.NoSelectionConverter.noSelectionValue".equals( arg2 ) ) { return null; } CustomerReferenceHelper customerReferenceHelper = ( CustomerReferenceHelper ) Contexts.getApplicationContext().get( "customerReferenceHelper" ); Integer id = Integer.parseInt( arg2 ); source = customerReferenceHelper.getVariable( id ); } catch ( NumberFormatException e ) { log.error( e, e ); } return variable; } @Override public String getAsString( FacesContext arg0, UIComponent arg1, Object arg2 ) { String result = null; VariableLookup variable= ( VariableLookup ) arg2; Integer id = variable.getId(); result = String.valueOf( id ); return result; } } </code></pre> <p>I've seen a few things about it possibly being the equals() method on the class, (that doesn't add up with everything else working, but I overrode it anyway as below, where the hashcode is just the id (id is a unique identifier for each item).</p> <p>Equals method:</p> <pre><code>@Override public boolean equals( Object other ) { if ( other == null ) { return false; } if ( this == other ) { return true; } if ( !( other instanceof VariableLookup ) ) { return false; } VariableLookup otherVariable = ( VariableLookup ) other; if ( this.hashCode() == otherVariable.hashCode() ) { return true; } return false; } </code></pre> <p>I'm at my wits end with this, I can't find what I could have missed?! Any help would be much appreciated.</p> <p>UPDATE: As I understand it, when the list is built it checks the binded value against each item to see if they match to set the selected item. Putting some debug statements in the equals method shows that all comparisons during the building of the list fail due to a comparison to a null object, but checking the seam debug page, it shows that the value is definitely set.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload