Wydanie konwertera JSF w SelectOneMenu [duplikat]
To pytanie ma już tutaj odpowiedź:
Błąd konwersji ustawienie wartości dla „konwertera zerowego” - Dlaczego potrzebuję konwertera w JSF? 2 odpowiedzijeszcze raz mam kłopoty. Chodzi mi o to, że w moim projekcie potrzebuję konwertera do (oczywiście) konwertowania elementów ze składnika SelectOneMenu na właściwość listy w odpowiednim komponencie bean. Na mojej stronie jsf mam:
<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>
A moja fasola to:
@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 ?
}
};
}
...
}
wgetAsObject () metoda, parametr value wydaje się być wartością, którą przekazałem do konstruktora SelectItem. Ale wgetAsString () metoda, wartość również wydaje się być reprezentacją ciągu Id. Ten parametr nie powinien być typu TdPublicType? W moim kodzie jest coś nie tak?