Load values from a SelectItem already saved on the screen

2

I'm having a problem loading the selected value into <f:selectItem> by the user when rendering looks like this:

<option value="320">Coordenador Desenvolvimento</option>

I wanted to set the attribute selected="selected"

Follow my xhtml:

<div class="span3">
    <h:selectOneMenu id="txttecnicosselecionaveis" required="true" 
                     requiredMessage="Selecione o tecnico responsável pelo cliente" 
                     binding="#{cadastradorCliente.txtTecnicosSelecionaveis}" disabled="#{cadastradorCliente.desabilitarCampos}" styleClass="span12">
        <f:selectItems value="#{cadastradorCliente.tecnicosSelecionaveis}" 
                       itemLabel="#{cliente.tecnico.nome}" itemValue="#{cliente.tecnico.id}"/>
    </h:selectOneMenu>
</div>

My MB where I retrieve data

public String actSelecionarTabela()
{
    FacesContext context = FacesContext.getCurrentInstance();
    HttpSession sessao = (HttpSession) context.getExternalContext().getSession(true);

    if (sessao != null) 
    {
        sessao = (HttpSession) context.getExternalContext().getSession(true);
        sessao.setAttribute("cliente", cliente);
    }

    txtCnpj.setValue(cliente.getCnpj());
    txtRazaoSocial.setValue(cliente.getRazaoSocial());
    txtNomeFantasia.setValue(cliente.getNomeFantasia());
    txtInscricaoEstadual.setValue(cliente.getInscricaoEstadual());
    txtSite.setValue(cliente.getSite());

    txtEndereco.setValue(cliente.getEndereco().getDescricao());
    txtResponsavel.setValue(cliente.getResponsavel().getNome());
    txtTelefone.setValue(cliente.getResponsavel().getContato().getTelefone());
    txtEmail.setValue(cliente.getResponsavel().getContato().getEmail());
    txtCemail.setValue(cliente.getResponsavel().getContato().getCemail());
    txtAtivo.setValue(cliente.isAtivo());       
    txtTecnicosSelecionaveis.setValue(cliente.getTecnico().getCodigo());
    txtGerentesSelecionaveis.setValue(cliente.getGerente().getCodigo());

When I return the data saved on the screen it returns my selectItem

I'dlikeittocomebackloadedvaluesthatwereselectedthefirsttime

Here is the result of data saved in bank

I have tried in many ways and I can not solve this, does anyone have any ideas?

    
asked by anonymous 23.01.2015 / 13:41

1 answer

2

The value property in selectOneMenu is missing.

For example, if your managed bean has the tecnicoResponsavel property indicating the previously selected technician you can do something like this:

<h:selectOneMenu id="txttecnicosselecionaveis" required="true" 
                     requiredMessage="Selecione o tecnico responsável pelo cliente" 
                     binding="#{cadastradorCliente.txtTecnicosSelecionaveis}"
                     disabled="#{cadastradorCliente.desabilitarCampos}" 
                     <!-- Carrega id do tecnico responsavel -->
                     value="#{cadastradorCliente.tecnicoResponsavel.id}"
                     styleClass="span12">

For a given item to be pre-selected, the value of the selectOneMenu must hit with the itemValue of the selectItems (in this case, #{cliente.tecnico.id} )

Fonts :

  • SOen - JSF: default selection for <f:selectItem> within <h:selectOneMenu>
  • SOen - JSF f: selectItems set item selected .
  • 23.01.2015 / 15:15