In my implementation, the select menu appears with populated value. But, if i selected any item from the menu, the select menu not setting the value and reset to default one.
Convertor:
package com.papar.common.converter;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.inject.Inject;
import org.springframework.stereotype.Component;
import com.papar.common.domain.Manufacturer;
import com.papar.common.repository.ManufacturerRepository;
@Component
@ManagedBean
@RequestScoped
public class ManufacturerConverter implements Converter {
@Inject
private ManufacturerRepository repository;
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (value == null || value.isEmpty()) {
return null;
}
try {
return repository.getById(Integer.valueOf(value));
} catch (Exception e) {
throw new ConverterException(new FacesMessage(String.format("Cannot convert %s to User", value)), e);
}
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (!(value instanceof Manufacturer)) {
return null;
}
return String.valueOf(((Manufacturer) value).getId());
}
// ...
}
JSF:
<p:column>Manufacturer</p:column>
<p:column>
<p:selectOneMenu converter="com.apt.papar.converter.ManufacturerConverter" value="#{brandBean.manufacturer}">
<f:selectItem itemLabel="Select Manufacturer" itemValue="-1"/>
<f:selectItems value="#{brandBean.manufacturers}" var="manufacturer" itemLabel="#{manufacturer.name}" itemValue="#{manufacturer.id}"/>
</p:selectOneMenu>
</p:column>
Please help..
Fix the three problems mentioned below:
@FacesConverter
annotation: as you're not using it you should instead use binding with an object: converter="#{manufacturerConverter}"
;itemValue
of <f:selectItems>
tag should point to an object, not to its id: itemValue="#{manufacturer}
, otherwise your converter usage would be wrong.Upon fixing, your <p:selectOneMenu>
will be working.