0
在我的實施中,選擇菜單顯示爲填充值。但是,如果我從菜單中選擇了任何項目,選擇菜單不會設置值並重置爲默認值。無法從SelectOneMenu中選擇任何項目
轉換器:
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>
請幫助..
+1。它的工作..非常感謝.. – Shashi
不客氣! – skuntsel