h:selectOneMenu not populating a 'selected' item

Posted by dann.dev on Stack Overflow See other posts from Stack Overflow or by dann.dev
Published on 2012-10-31T04:57:25Z Indexed on 2012/10/31 5:00 UTC
Read the original article Hit count: 156

Filed under:
|

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.
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.

My selectOneMenu looks like this:

<h:selectOneMenu id="selVariable" 
     value="#{customer.variableLookup}" 
     converter="#{variableLookupConverter}">
    <s:selectItems var="source" 
        value="#{customerReferenceHelper.variableLookups()}" 
        label="#{source.name}" />
</h:selectOneMenu>

And the converter is below. It very simple and just turns the id from string to int and back etc:

@Name( "sourceOfWealthLookupConverter" )
public class SourceOfWealthLookupConverter 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;
 }
}

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).

Equals method:

@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;
}

I'm at my wits end with this, I can't find what I could have missed?! Any help would be much appreciated

© Stack Overflow or respective owner

Related posts about seam2

Related posts about selectonemenu