0

我做了几次测试,每当我查看来自@BalusC 的几篇帖子时都会出现转换错误,并且错误仍然存​​在。还使用了 GenericEnumConverter @BalusC

我的枚举

public enum EnumTipoPapel {

CONTATO(1,"Contato"),
DIRETOR(2,"Diretor"),
PASSAGEIRO(3,"Passageiro"),
COMANDANTE(4,"Comandante"),
ACOMPANHANTE(5,"Acompanhante"),
VISITANTE(6, "Visitante"),
SECRETARIA_GERAL(7,"Secretaria Geral"),
ANIVERSARIANTE(99,"Aniversariante");

private int codigo;
private String descricao;

EnumTipoPapel(int codigo, String descricao){
    this.codigo = codigo;
    this.descricao = descricao;
}

/**
 * @return the codigo
 */
public int getCodigo() {
    return codigo;
}

@Override
public String toString() {
    return descricao;
}   

}

我的jsf页面

<h:selectManyCheckbox id="cadastrarPessoaPapeis" styleClass="select_table" value="#{cadastrarPessoasBean.papeis}">
     <f:converter converterId="tipoPapelEnum"/>
     <f:selectItems value="#{cadastrarPessoasBean.selectedPapeis}"/>
</h:selectManyCheckbox>

我的豆子

public class CadastrarPessoasBean extends AbstractBean {
private IPessoa iPessoa = (IPessoa) BradescoSpringUtils.getBeanById("pessoaBO");

private SelectItem[] papeis;
private EnumTipoPapel[] selectedPapeis;

public CadastrarPessoasBean() {
    this.initValues();
}

/**
 * Realiza o load de todos os campos necessario para o carregamento da tela
 */
public void initValues() {
    loadPapeis();
}


/*********************************************************************************
 * Componentes de tela
 ********************************************************************************/


/*********************************************************************************
 * get and set
 ********************************************************************************/


public SelectItem[] getPapeis() {
    return papeis;
}

public void setPapeis(SelectItem[] papeis) {
    this.papeis = papeis;
}

/*********************************************************************************
 * Acoes da tela
 ********************************************************************************/

public EnumTipoPapel[] getSelectedPapeis() {
    return selectedPapeis;
}

public void setSelectedPapeis(EnumTipoPapel[] selectedPapeis) {
    this.selectedPapeis = selectedPapeis;
}

/**
 * realiza o load dos papeis na pagina
 */
private void loadPapeis() {
    this.papeis = new SelectItem[] {
        new SelectItem(EnumTipoPapel.ANIVERSARIANTE),
        new SelectItem(EnumTipoPapel.DIRETOR),
        new SelectItem(EnumTipoPapel.PASSAGEIRO),
        new SelectItem(EnumTipoPapel.COMANDANTE),
        new SelectItem(EnumTipoPapel.SECRETARIA_GERAL)
    };
}

}

我的转换器

public class TipoPapelConverter implements Converter {

public Object getAsObject(FacesContext context, UIComponent component, String value) {
    return EnumTipoPapel.valueOf(value);
}

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

}

我的面梁配置

<converter>
    <converter-id>tipoPapelConverter</converter-id>
    <converter-class>....converter.TipoPapelConverter</converter-class>
</converter>
<converter>
    <converter-id>genericEnumConverter</converter-id>
    <converter-class>...converter.GenericEnumConverter</converter-class>
</converter>

例外

Value is not an enum: class javax.faces.model.SelectItem
4

1 回答 1

1

有几个错误:

  1. 您不应该将SelectItem实例绑定到<h:selectManyCheckbox value>. 他们应该只受约束<f:selectItem(s) value>

  2. 您应该null在转换器中执行检查。

总而言之,这个启动示例应该这样做:

<h:selectManyCheckbox value="#{bean.selectedPaperTypes}" converter="paperTypeConverter">
     <f:selectItems value="#{bean.availablePaperTypes}" />
</h:selectManyCheckbox>

在豆中:

private PaperType[] selectedPaperTypes;
private SelectItem[] availablePaperTypes;

public void init() {
    PaperType[] paperTypes = PaperType.values();
    SelectItem[] availablePaperTypes = new SelectItem[paperTypes.length];

    for (int i = 0; i < paperTypes.length; i++) {
        availablePaperTypes[i] = new SelectItem(paperTypes[i]);
    }
}

在转换器中:

public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) {
    return (submittedValue != null) ? PaperType.valueOf(value) : null;
}

public String getAsString(FacesContext context, UIComponent component, Object modelValue) {
    return (value instanceof PaperType) ? ((PaperType) value).name() : null;
}
于 2013-10-30T10:49:06.367 回答