0

I'm using JSF 2.1 and Tomcat 7. I have the following PrimeFaces select one menu:

<p:selectOneMenu id="idMarcaEdit" 
                 value="#{cfgCentraleController.selMarcaEdit}" 
                 var="p"
                 height="250" 
                 effect="fade"
                 converter="marcaConverter"
                 >
    <f:selectItems value="#{cfgCentraleController.marche}" var="c" itemLabel="#{c.marca}-#{c.modello}-#{c.versione}"  itemValue="#{c}"/>  
    <p:column>
        <h:outputText value="#{p.marca}" />
    </p:column>
    <p:column>
        <h:outputText value="#{p.modello}" />
    </p:column>
    <p:column>
        <h:outputText value="#{p.versione}" />
    </p:column>
    <p:column>
        <h:outputText value="#{p.provisioning}" />
    </p:column>
</p:selectOneMenu>

I have this in #{cfgCentraleController}

private List<Marca> marche;
private Marca selMarcaEdit;

public Marca getSelMarcaEdit() {
    return selMarcaEdit;
}

public void setSelMarcaEdit( Marca selMarcaEdit ) {
    this.selMarcaEdit = selMarcaEdit;
    this.selectedCentrale.setIdRete( this.selMarcaEdit.getIdMarca());
}

public List<Marca> getMarche() {
    return marche;
}

It works fine, but I don't realy need the whole entity to be submitted. I just need its ID.

I'm using OmniFaces for the converter:

import org.omnifaces.converter.SelectItemsConverter;

@Override
public String getAsString( FacesContext context, UIComponent component, Object value ) {
    Integer id = (value instanceof Marca) ? ((Marca) value).getIdMarca() : null;
    return (id != null) ? String.valueOf( id ) : null;
}

How do I obtain only the ID of the entity in the model?

user1594895
  • 587
  • 1
  • 10
  • 31

1 Answers1

0

If you need only the ID, then just set only the ID as select item value.

Change

<f:selectItems ... itemValue="#{c}" />

to

<f:selectItems ... itemValue="#{c.idMarca}" />

and change

private Marca selMarcaEdit;

to

private Integer selMarcaEdit;

then you can remove the converter from <p:selectOneMenu>.

See also:


Update: this is not possible in this construct with menu with "custom layout". After all, it turns out that the <p:selectOneMenu var> is getting its value from <f:selectItems itemValue> instead of <f:selectItems var>. Sorry, I totally didn't expect that. Given this, you cannot change the model and get rid of the converter. You should keep the code as is. Your best bet is to post an enhancement request to PF guys to improve this situation.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Ok but obviously now i can't show the "extended data of the Object", I get Property 'idRete' not found on type java.lang.Integer. So if I need the description How to achive this? – user1594895 Jun 24 '13 at 12:32
  • Huh? Did you change the `var` instead of `itemValue` or so? – BalusC Jun 24 '13 at 12:33
  • No i have done as you say but Can't be done on an Integer. Using an Integer I can't get value of single element, so How to ? – user1594895 Jun 24 '13 at 13:00
  • It look like that `` is depending on ``. I didn't expect that. Well, bad luck. You're going to keep the converter anyway. Your best bet is to post an enhancement request to PF guys to improve this situation. – BalusC Jun 24 '13 at 13:05
  • Uh thanks now i try with standard JSF. I have never thought about that possibility. – user1594895 Jun 24 '13 at 13:13
  • This can also be done with ``. Just remove `` and nested `` altogether. – BalusC Jun 24 '13 at 13:16