开发者

Injecting Resources Into UIComponent (aka does CDI work here?)

I am writing a (composite) component that needs to interact with my DAO. Here is how the Java part is declared:

@FacesComponent(value="selectLocation")
public class SelectLocation extends UINamingContainer {

To get the DAO object, I tried the CDI annotation:

    @Inject private LocationControl lc;

And that didn't work so I tried the Faces annotation:

    @ManagedProperty (value = "@{locationControl}") private LocationControl lc;

Both cases nothing happens -- the property lc ends up as null after the constructor finishes.

I use CDI in all my backing beans and it all works. This would be using Weld inside GlassFish 3.1.1. Any suggestions 开发者_运维知识库on how to get the resource?


I have a work-around for now, which is to basically put in the boiler-plate code that CDI et. al. is supposed to do away with. I now have this method:

public LocationControl getLocationControl() {
    if (lc != null) return lc;
    FacesContext fc = getFacesContext();
    Object obj = fc.getApplication().evaluateExpressionGet(fc, "#{locationControl}", LocationControl.class);
    if (obj instanceof LocationControl) lc = (LocationControl) obj;
    return lc;
}

I would like to know if anyone has a better solution.


I don't know if it also works for components, but with CDI + MyFaces CODI you have @Advanced to mark e.g. Phase-Listeners which should be able to use @Inject. If it doesn't work, you could create a feature request in their JIRA. They are pretty fast and there are frequent releases.

Or you use: MyBean myBean = BeanManagerProvider.getInstance().getContextualReference(MyBean.class); manually.


There is a way to do this work without workarounds?

Yes, just use a backing bean the usual way.

<x:someComponent value="#{someBean.someProperty}" />

Wrap if necessary in a reusable tagfile/composite to keep it DRY:

<my:someComponent />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜