每当我查看@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();
}
}
my faces-beam config
<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
答案 0 :(得分:1)
有几个错误:
您不应将SelectItem
个实例绑定到<h:selectManyCheckbox value>
。它们应该仅限于<f:selectItem(s) value>
。
您应该在转换器中执行null
检查。
总而言之,这个启动示例应该:
<h:selectManyCheckbox value="#{bean.selectedPaperTypes}" converter="paperTypeConverter">
<f:selectItems value="#{bean.availablePaperTypes}" />
</h:selectManyCheckbox>
with in bean:
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;
}