View Scope in CDI Weld
I want to use the @ViewScoped
开发者_如何学Python - scope in my application for the backing beans of some web pages. Also I use CDI to inject the dependecies into the backing beans.
However, when I use a backing bean annotated like this
@ManagedBean
@ViewScoped
@Inject
someDependency (...)
the @Inject
annotation will not inject anything and i get a NullPointerException
as soon as i am accessing the dependency.
However, when I decorate the backing bean with
@Named
@ViewScoped
@Inject
someDependency (...)
the injection works fine, but now the @ViewScoped
is ignored as it is not part of CDI / Weld.
How can I use @ViewScoped
together with CDI Weld?
The problem is that you are mixing simple managed beans with CDI managed beans and they don't work together. Managed Beans is a simple framework for defining beans and their injected beans. CDI is a separate beast with all sorts of extra goodness.
However, Managed beans can't use CDI Injection points but can use the ViewScope while CDI beans use CDI injection points and all that good stuff but the ViewScope isn't available.
To resolve the issue you have to either go with CDI and use the Seam-Faces library to use view scope, or drop CDI and stick with simple managed beans which is a simple implementation.
Cheers,
Andy
You can get @javax.faces.bean.ViewScoped
to work by including the Seam Faces 3.1.0 jar in your project.
Failing that (i.e. you're using GlassFish 3.1.1 or earlier), you can simply copy ViewContextExtension.java, ViewScopedContext.java and javax.enterprise.inject.spi.Extension from Seam Faces 3.1.0 into your own project, ensuring you use the same path to the files as Seam Faces does. The java files can be copied verbatim. All lines except the one ending with ViewContextExtension
should be removed from javax.enterprise.spi.Extension.
I am using the latter method successfully in GlassFish 3.1.1 and will try the former method one GlassFish 3.1.2 is released.
No, it's not directly supported. Seam3 is supposed to provide such extras that CDI does not. Check it out.
You can implement the @NormalScope to create your own CDI Scope witout using any other framework or waiting for new JEE7
- CDI fires an event AfterBeanDiscovery after each bean call
- You can use CDI extension to @Observes this event and add you context implementation
- In your scope implementation you can :
- Use Contextual to get your bean by its name from FacesContext ViewRoot Map and return it after each ajax call back
- Use CreationalContext if the bean name from first step is not found to create it in the FacesContext ViewRoot Map
for a more in-depth explanation i recommand this link : http://www.verborgh.be/articles/2010/01/06/porting-the-viewscoped-jsf-annotation-to-cdi/
I don't use Seam, just normal JSF + PrimeFaces. I just found this and I am going to give it a try... you might want to as well.
Weld in combination with Seam-Faces would provide it but it's broken. An interesting thread about it and an alternative for it is e.g. at http://forum.primefaces.org/viewtopic.php?f=3&t=7585
I think Apache CODI or Seam 3 solves this. There is a new project called DeltaSpike that might be doing this, think it continues Seam 3.
In Java EE 7, this problem will be solved as I understand that all beans are CDI beans, so there is no JSF beans.
精彩评论