Dúvida com HtmlSelectOneMenu e UISelectItem, deixando o option com selected em JSF

3 respostas
SanjuanRJ
Em html puro é bem simples de fazer:
<SELECT NAME="partnumber">
<OPTION VALUE="7382"          >steam turbine
<OPTION VALUE="2928"          >resistor array
<OPTION VALUE="3993" SELECTED >widget analyzer
<OPTION VALUE="9398"          >fiber identifier
</SELECT>
Mas em Java Server Faces não consigo fazer isso....estava tentando fazer algo mais ou menos assim
for(BeanVendedor vendedor: vendedores)
            {
                UISelectItem selectVendedor = new UISelectItem();
                selectVendedor.setItemLabel(vendedor.getNome());
                selectVendedor.setItemValue(vendedor.getNome());
                
                /*
                if(vendedorEscolhido.getId() == vendedor.getId())
                {
                    selectVendedor.setValue("SELECTED");
                }
                */
                smVendedores.getChildren().add(selectVendedor);
            }
mas não funciona... ajudem-me por favor :cry:

3 Respostas

SanjuanRJ

Por favor pessoal. Qualquer dica é bem vinda.

:slight_smile:

SanjuanRJ

Ninguém nunca passou por essa sitauação? :roll:

C

Eu já passei, e achei meio trabalhoso, lá vai:

No meu caso eu jogo um objeto do tipo ParticipanteTipo no combobox.

no meu bean:

...
	private ParticipanteTipo tipo;

	//eu suprimi os gets e sets para o tipo, pois sao gets e sets simples.

	private List<SelectItem> selectItemsPartTipo;

	public List<SelectItem> getSelectItemsPartTipo() {
		if (selectItemsPartTipo==null) {
			selectItemsPartTipo = new ArrayList<SelectItem>();
			
			ParticipanteTipo nulo = new ParticipanteTipo();
			selectItemsPartTipo.add(new SelectItem(nulo, ""));
			
			List<ParticipanteTipo> todos = getParticipanteService().getParticipanteTipoDao().listAll();
			for (int i=0; i<todos.size(); i++) {
				ParticipanteTipo pt = todos.get(i);
				selectItemsPartTipo.add(new SelectItem(pt, pt.getDescricao()));
			}			
		}		
		return selectItemsPartTipo;
	}

	public void setSelectItemsPartTipo(List<SelectItem> selectItemsPartTipo) {
		this.selectItemsPartTipo = selectItemsPartTipo;
	}
...

No arquivo .xhtml:

<a4j:form ajaxSubmit="true">
					<div align="center">
						<rich:panel styleClass="login" bodyClass="login_body">
							<f:facet name="header">
								<h:outputText value="Novo Cadastro"/>
							</f:facet>
							<h:outputText value="#{msgsadmpart.clubePremiosPartCampoTipo}" styleClass="bold" />:<br />
							<h:selectOneMenu id="tipo" value="#{novoParticipanteBean.tipo}" style="width:206px;"> 
							     <f:selectItems value="#{novoParticipanteBean.selectItemsPartTipo}"/> 
							     <a4j:support event="onchange" action="#{novoParticipanteBean.alterarParticipanteTipo}" ajaxSingle="true" reRender="panelDadosComplementares"/>
							</h:selectOneMenu>
							<br /><br />
							<a4j:commandButton value="Continuar" action="#{loginBean.login}" onclick="javascript:Richfaces.showModalPanel('mp_novoparticipante',{width:584, top:200});"></a4j:commandButton>				
						</rich:panel>
					</div>
					</a4j:form>

Agora vem a parte diferente… Vc tem que criar um converter para ParticipanteTipo.

No faces-config.xml

<converter>
    <converter-for-class>br.com.atualinformatica.clubedepremios.model.ParticipanteTipo</converter-for-class>
    <converter-class>br.com.atualinformatica.sisweb.web.jsf.BaseConverter</converter-class>
  </converter>

Eu criei um converter genérico. Não sei se é a melhor prática, mas lá vai o código dele

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

public class BaseConverter implements Converter  {

	public Object getAsObject(FacesContext context, UIComponent component, String value) {

		Object obj = component.getAttributes().get(value);
		if (obj instanceof IValueObject) {
			IValueObject vo = (IValueObject)obj;
			if (vo.getId()!=null) {
				return obj;
			} else {
				return null;
			}
		} else {		
			return component.getAttributes().get(value);
		}
	}

	public String getAsString(FacesContext context, UIComponent component, Object value) {
		return value.toString();
	}
	
}

Bom, tudo isso vai fazer funcionar e trazer selecionado o item, desde que o objeto tipo esteja != null

T+

Claudiney

Criado 24 de março de 2008
Ultima resposta 25 de mar. de 2008
Respostas 3
Participantes 2