I have problems again. I want to say the following: In my project, I need a converter to (obviously) convert elements from the SelectOneMenu component to a list property in the corresponding bean. On my jsf page, I have:
<p:selectOneMenu id="ddlPublicType" value="#{publicBean.selectedPublicType}" effect="fade" converter="#{publicBean.conversor}" > <f:selectItems value="#{publicoBean.lstPublicTypes}" var="pt" itemLabel="#{pt.label}" itemValue="#{pt.value}"></f:selectItems> </p:selectOneMenu>
And my bean:
@ManagedBean(name = "publicBean") @RequestScoped public class PublicBean { // Campos private String name; // Nome do evento private TdPublicType selectedPublicType = null; private List<SelectItem> lstPublicTypes = null; private static PublicTypeDAO publicTypeDao; // DAO static { publicTypeDao = new PublicTypeDAO(); } // Construtor public PublicoBean() { lstPublicTypes = new ArrayList<SelectItem>(); List<TdPublicType> lst = publicTypeDao.consultarTodos(); ListIterator<TdPublicType> i = lst.listIterator(); lst.add(new SelectItem("-1","Select...")); while (i.hasNext()) { TdPublicType actual = (TdPublicType) i.next(); lstPublicTypes.add(new SelectItem(actual.getIdPublicType(), actual.getNamePublicType())); } } // Getters e Setters ... public Converter getConversor() { return new Converter() { @Override public Object getAsObject(FacesContext context, UIComponent component, String value) { // This value parameter seems to be the value i had passed into SelectItem constructor TdPublicType publicType = null; // Retrieving the PublicType from Database based on ID in value parameter try { if (value.compareTo("-1") == 0 || value == null) { return null; } publicType = publicTypeDao.findById(Integer.parseInt(value)); } catch (Exception e) { FacesMessage msg = new FacesMessage("Error in data conversion."); msg.setSeverity(FacesMessage.SEVERITY_ERROR); FacesContext.getCurrentInstance().addMessage("info", msg); } return publicType; } @Override public String getAsString(FacesContext context, UIComponent component, Object value) { return value.toString(); // The value parameter is a TdPublicType object ? } }; } ... }
In the getAsObject () method, the value parameter is represented by the value that I passed to the SelectItem constructor. But in the getAsString () method, the value also appears as a string representation of Id. Should this parameter not be of type TdPublicType? Is there something wrong in my code?
source share