View Scope in CDI Weld
Asked Answered
Z

7

11

I want to use the @ViewScoped - 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?

Zannini answered 1/2, 2011 at 16:18 Comment(0)
T
12

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

Taligrade answered 25/2, 2011 at 20:14 Comment(0)
W
7

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.

Wildfire answered 2/1, 2012 at 13:20 Comment(0)
S
1

No, it's not directly supported. Seam3 is supposed to provide such extras that CDI does not. Check it out.

Swound answered 3/2, 2011 at 9:55 Comment(3)
Thanks for your response. It's really strange that there's no ViewScope in CDI. If I do not want to add further frameworks or libraries to my project, how can I achieve the following: I have a backing bean for a jsf page. This bean saves some input from the user (so the bean should not be destroyed during this!) and after the user hits submit, the input is processed and the bean may be destroyed. I do not want to use session scope or application scope. Can I achieve this somehow with standard CDI, maybe conversation scope? I mean it's a very basic situation, it should be possible somehowZannini
seam3 final release just came out. Do u know if it completely support GF 3.1. I know there were couple problem with GF 3.1 before the final release, but I am not sure if the final release fix them allPredominant
Seam Faces 3.1.0 with GlassFish 3.1.1 is still a no-go. It only works if you patch GlassFish with the latest Weld, but for many that is not practical. GlassFish 3.1.2 will contain an update for Weld (among other things) and the first release candidate is due at the end of Jan 2012.Wildfire
F
1

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 :
    1. Use Contextual to get your bean by its name from FacesContext ViewRoot Map and return it after each ajax call back
    2. 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/

Foozle answered 26/4, 2013 at 10:20 Comment(1)
very interresting implementation .Aloft
W
0

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.

Womanizer answered 26/7, 2011 at 12:15 Comment(2)
Don't use this implementation it isn't correct at all. A correct one can be found e.g. at goo.gl/sWdcl The context itself at goo.gl/2oRp3Glochidium
The above goo.gl links are myfaces.apache.org/extensions/cdi and svn.apache.org/repos/asf/myfaces/extensions/cdi/trunk/…)Ibrahim
G
0

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

Glochidium answered 25/8, 2011 at 15:50 Comment(0)
T
0

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.

Trothplight answered 20/4, 2013 at 23:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.