组件中的转换错误使用JSF的Converter

时间:2015-07-10 17:36:01

标签: jsf jsf-2 converter

我有一个问题是使用ConvertGeneric捕获在组合中选择的对象,这是一个异常无法转换的释放。有没有人有发言权。

  

javax.servlet.ServletException:javax.el.E​​LException:/app/administrador/CadastrarUsuario.xhtml @ 145,79 value =“#{usuarioController.user.roles}”:无法转换com.app.entity.Perfil @类型为amc.scda.entity.Perfil的类型为20,用于接口java.util.List

<h:selectOneMenu id="cbxPerfil" required="true"
   requiredMessage="#{rotulos.perfil_mandatory}"
   value="#{userController.user.roles}" converter="genericConverter">
        <f:selectItem itemLabel="Selecione" itemValue="#{null}" />
        <f:selectItems value="#{userController.listPerfilSelect}" />
    <f:ajax render="cbxPerfil" event="valueChange"/>
</h:selectOneMenu>
public interface SampleEntity {

    public Integer getId();

}
@FacesConverter(value = "genericConverter", forClass = SampleEntity.class)
public class GenericConverter implements Converter, Serializable {

    private static final long serialVersionUID = -6461922703911161947L;

    @Override
    public Object getAsObject(FacesContext ctx, UIComponent component, String value) {
        if (value != null) {
            return this.getAttributesFrom(component).get(value);
        }
        return null;
    }

    @Override
    public String getAsString(FacesContext ctx, UIComponent component, Object value) {

        if (value != null && !"".equals(value)) {

            SampleEntity entity = (SampleEntity) value;

            // adiciona item como atributo do componente
            this.addAttribute(component, entity);

            Integer codigo = entity.getId();
            if (codigo != null) {
                return String.valueOf(codigo);
            }
        }

        return (String) value;
    }

    protected void addAttribute(UIComponent component, SampleEntity o) {
        String key = o.getId().toString();
        this.getAttributesFrom(component).put(key, o);
    }

    protected Map<String, Object> getAttributesFrom(UIComponent component) {
        return component.getAttributes();
    }

}
@Entity
@Table(name = "roles")
public class Perfil implements GrantedAuthority, SampleEntity {
    private static final long serialVersionUID = -3968396919486158590L;

    private Integer id;
    private String description;

    @Id
    @SequenceGenerator(name = "role_id_seq", sequenceName = "role_id_seq", allocationSize = 1)
    @GeneratedValue(generator = "role_id_seq", strategy = GenerationType.SEQUENCE)
    @Column(name = "role_id")
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Column(name = "role_description", unique = true)
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    @Transient
    public String getAuthority() {
        return description;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Perfil other = (Perfil) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        return true;
    }

}

0 个答案:

没有答案