将 CDI 注入 FacesConverter [英] CDI Injection into a FacesConverter

查看:15
本文介绍了将 CDI 注入 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 容器.任何人都可以在我重新编码以不使用转换器之前提出解决方案吗?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆