【发布时间】:2023-04-07 18:32:02
【问题描述】:
从几次搜索来看,这似乎是一个已经存在一段时间的问题。我编写了一个 FacesConverter,如下所示。 Category 对象是一个 JPA 实体,CategoryControl 是获取它的 DAO。
@FacesConverter(value = "categoryConverter")
public class CategoryConverter implements Converter {
@Inject private CategoryControl cc;
public CategoryConverter() { }
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (cc != null) return cc.getByName(value);
System.out.println("CategoryConverter().getAsObject(): no injection!");
return null;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (!(value instanceof Category)) return null;
return ((Category) value).getName();
}
}
您现在可能已经猜到了,我从来没有注射过。我从this page 得到了这个解决方法,看起来像这样。:
Workaround for this problem: create this method in your localeController:
public Converter getConverter()
{
return FacesContext.getCurrentInstance().getApplication().createConverter("localeConverter");
}
and use converter="#{localeController.converter}" in your h:selectOneMenu.
但是我也无法完成这项工作。我的支持 bean 可以创建并返回一个转换器,但它没有将对象注入其中。
我正在使用 MyFaces CODI 1.0.1。使用当前的 GlassFish/Weld 容器。在我重新编码不使用转换器之前,任何人都可以提出解决方案吗?
【问题讨论】: