我做了几次测试,每当我查看来自@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