在我的实现中,选择菜单显示为填充值。但是,如果我从菜单中选择任何项目,则选择菜单不会设置值并重置为默认值。
转换器:
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>
请帮助..
答案 0 :(得分:2)
修复下面提到的三个问题:
@FacesConverter
注释设置转换器的ID:因为您没有使用它,所以您应该使用与对象的绑定:{{1} }; converter="#{manufacturerConverter}"
itemValue
标记应指向一个对象,而不是其ID:<f:selectItems>
,否则您的转换器使用会出错。
醇>
修正后,您的itemValue="#{manufacturer}
将正常运作。