将CDI注入FacesConverter [英] CDI Injection into a FacesConverter
问题描述
通过几次搜索,这似乎已经存在了一段时间.我写了一个FacesConverter,看起来像下面的样子.对象 Category 是一个JPA实体,而 CategoryControl 是获取它的DAO.
From just a few searches, this seems like a problem that has been around for a while. I have written a FacesConverter that looks like the following. The object Category is a JPA entity and CategoryControl is the DAO that fetches it.
@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();
}
}
您现在可能已经猜到了,我从来没有得到过注射.我从此页面(如下所示)中获得了解决方法:
As you probably guessed by now, I never get the injection. I got this workaround from this page, which looks like this.:
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可以创建并返回一个转换器,但是它没有将对象注入其中.
However I can't make this work either. My backing bean creates and returns a converter all right, but it doesn't get the object injected into it.
我正在使用MyFaces CODI 1.0.1.使用当前的GlassFish/Weld容器.在我重新编码为不使用Converter之前,有人可以提出解决方案吗?
I am using MyFaces CODI 1.0.1. With the current GlassFish/Weld container. Can anyone suggest a solution before I re-code to not use a Converter?
推荐答案
替换
@FacesConverter(value = "categoryConverter")
作者
@Named
并使用
<h:inputSomething converter="#{categoryConverter}" />
或
<f:converter binding="#{categoryConverter}" />
代替
<h:inputSomething converter="categoryConverter" />
或
<f:converter converterId="categoryConverter" />
顺便说一句,@FacesConverter
中的@EJB
存在类似的问题.但是,它提供了JNDI手动抓取的方法.另请参见 JSF 2.0中的通信-在@FacesConverter中获取EJB和@FacesValidator .这样,您可以使用@FacesConverter(forClass=Category.class)
,而无需每次都手动定义它.不幸的是,我无法从头顶上知道如何实现CDI bean.
By the way, similar problem exist for @EJB
inside a @FacesConverter
. It however offers a way to be grabbed by JNDI manually. See also Communication in JSF 2.0 - Getting an EJB in @FacesConverter and @FacesValidator. This way you can use a @FacesConverter(forClass=Category.class)
without manually defining it everytime. Unfortunately I can't tell from top of head how to realize that for CDI beans.
更新:如果您恰巧使用JSF实用程序库 OmniFaces ,因为添加了1.6版透明支持在@FacesConverter
类中使用@Inject
和@EJB
,而无需任何其他配置或注释.另请参见 CDI @FacesConverter
展示示例.
Update: if you happen to use JSF utility library OmniFaces, since version 1.6 is adds transparent support for using @Inject
and @EJB
in a @FacesConverter
class without any additional configuration or annotations. See also the CDI @FacesConverter
showcase example.
这篇关于将CDI注入FacesConverter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!