When I use f:selectItems the itemLabel doesnt show the property descricao, but show the toString(). I've made some researches, but the problem continues. <f:selectItems> only shows toString() of the model as item label
What am I doing wrong? Any ideas?
I have a class Tipo as follow:
public class Tipo implements Serializable{
/**
*
*/
private static final long serialVersionUID = -763536865855419703L;
// descrição do tipo
private String descricao;
// código do tipo
private Long tipoId;
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException cnse) {
return null;
}
}
public Tipo(Long id) {
this.tipoId = id;
}
public Tipo() {
}
public String getDescricao() {
return descricao;
}
public Long getTipoId() {
return tipoId;
}
public void setDescricao(String umaDesc) {
this.descricao = umaDesc;
}
public void setTipoId(Long id) {
this.tipoId = id;
}
public String toString() {
return " ID=" + this.getTipoId() + ", Descricao=" + this.getDescricao();
}
@Override
public boolean equals(Object other){
return (other != null && getClass() == other.getClass() && tipoId != null)
? tipoId.equals(((Tipo) other).tipoId) : (other == this);
}
@Override
public int hashCode() {
return (tipoId != null)
? (getClass().hashCode() + tipoId.hashCode()) : super.hashCode();
}
}
And a TipoDAOImpl:
public class TipoDAOImpl extends NamedParameterJdbcDaoSupport implements TipoDAO, Serializable {
private static final long serialVersionUID = 8698127647660788120L;
private SimpleJdbcInsert sji;
@Value("#{queries.sql03}")
private String sql03;
@Value("#{queries.sql04}")
private String sql04;
@Override
public List<Tipo> getTodosTipos() throws DAOException {
try {
RowMapper<Tipo> mapper = getRowMapper();
return getJdbcTemplate().query(this.sql03, mapper);
} catch (EmptyResultDataAccessException ex) {
throw new DAOException("Não há registros na tabela de tipos.");
} catch (DataAccessException e) {
throw new DAOException(e.getMessage());
}
}
private RowMapper<Tipo> getRowMapper() {
RowMapper<Tipo> mapper = new RowMapper<Tipo>() {
public Tipo mapRow(ResultSet rs, int rowNum) throws SQLException {
Tipo t = new Tipo();
t.setTipoId(rs.getLong("tipo_id"));
t.setDescricao(rs.getString("descricao"));
return t;
}
};
return mapper;
}
protected SimpleJdbcInsert getSji() {
return sji;
}
protected void setSji(SimpleJdbcInsert sji) {
this.sji = sji;
}
}
ManagedBean:
@ManagedBean
@SessionScoped
public class TipoMB extends ManagedBeanBasico implements Serializable{
private static final long serialVersionUID = 2482494734070978599L;
@ManagedProperty(name = "tipoFacade", value = "#{tipoFacade}")
private TipoFacade tipoFacade;
private List<Tipo> listTipos;
private Tipo tipo;
public List<Tipo> getTodosTipos(){
try {
listTipos = tipoFacade.getTodosTipos();
} catch (DAOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return listTipos;
}
Converter:
@FacesConverter(value="tipoConverter")
public class TipoConverter implements Converter {
@EJB private Tipo tipo;
@EJB private TipoFacade tipoFacade;
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value)
throws ConverterException {
try {
return tipoFacade.getTipoPorId(Long.parseLong(value));
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (DAOException e) {
e.printStackTrace();
}
return value;
}
@Override
public String getAsString(FacesContext context, UIComponent component,
Object value) throws ConverterException {
if (value == null) {
return "";
}
if (!(value instanceof Tipo)) {
throw new ConverterException("Não é um tipo válido " + value );
}
return ((Tipo) value).getTipoId().toString();
}
form.xhtml:
<h:outputText value="TIPO:"/>
<p:selectOneMenu value="#{publicacaoMB.publicacao.tipo}" converter="tipoConverter">
<f:selectItems value="#{tipoMB.listTipos}" var="tipo"
itemLabel="#{tipo.descricao}" itemValue="#{tipo.tipoId}"/>
</p:selectOneMenu>