Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>CDI has no view scope, because it <a href="https://stackoverflow.com/questions/7812985/cdi-missing-viewscoped-and-flashscoped">doesn't have the notion of a view</a>, so if you need that scope, CDI in its pure form can't do it. View scope basically means <a href="https://stackoverflow.com/questions/5220407/viewscoped-works-like-requestscoped-why/5220668#5220668">request scope + being AJAX-ready</a>. It's <strong>not</strong> a JSF view, like a page named <code>xyz.xhtml</code>, even though you see JSF <code>&lt;f:viewParam&gt;</code> and the likes. A frequent use case with view-scoped beans is <a href="http://balusc.blogspot.com/2011/09/communication-in-jsf-20.html#ProcessingGETRequestParameters" rel="nofollow noreferrer">how to get GET parameters into a such a bean</a>. Also <a href="https://stackoverflow.com/questions/4888942/viewparam-vs-managedpropertyvalue-param-id">read this</a>.</p> <p>Note that CDI rather lives at the EJB/service layer than the JSF/presentation layer. <a href="http://www.hascode.com/2011/02/creating-a-sample-java-ee-6-blog-application-with-jpa-ejb-cdi-jsf-and-primefaces-on-glassfish/" rel="nofollow noreferrer">This blog</a> has a nice overview.</p> <p>As such <code>@ManagedBean</code> cannot be fully replaced by CDI, again if you're using <code>@ViewScoped</code> beans - at least not without <a href="http://www.verborgh.be/articles/2010/01/06/porting-the-viewscoped-jsf-annotation-to-cdi/" rel="nofollow noreferrer">extending CDI</a> or using the <a href="http://docs.jboss.org/seam/3/faces/latest/reference/en-US/html/scopes.html#viewscoped" rel="nofollow noreferrer">Seam 3 Faces module</a>. Using view-scoped beans is almost always going to happen when using AJAXed JSF 2-based GUI toolkits like RichFaces, PrimeFaces, IceFaces etc.</p> <p>Mixing annotations from the wrong Java EE 6 packages <em>can</em> get you in trouble unexpectedly, again when using RichFaces or a similar API:</p> <pre><code>@javax.faces.bean.ManagedBean @javax.faces.bean.[Jsf]Scoped </code></pre> <p>are for components used <strong>solely</strong> at the presentation layer, here by RichFaces, PrimeFaces, etc. Some rich components <a href="http://community.jboss.org/thread/173633" rel="nofollow noreferrer">seem to have problems with CDI-annotated and JSF-annotated helper beans</a>. If you get strange behavior from your beans (or beans that seem to do nothing) the wrong mix of annotations might be the cause.</p> <p>Mixing JSF and CDI, like</p> <pre><code>@javax.inject.Named @javax.faces.bean.[Jsf]Scoped </code></pre> <p>is possible and works in most cases when referenced from JSF pages, however there are some little-known issues/drawbacks, e.g. when <a href="https://stackoverflow.com/questions/7723417/getting-a-get-request-param-into-an-viewscoped-bean">using a JSF scope which CDI doesn't have</a>:</p> <blockquote> <p>Also the combination <code>@Named @ViewScoped</code> won't work as intended. The JSF-specific <code>@ViewScoped</code> works in combination with JSF-specific <code>@ManagedBean</code> only. Your CDI-specific <code>@Named</code> will behave like <code>@RequestScoped</code> this way. Either use <code>@ManagedBean</code> instead of <code>@Named</code> or use CDI-specific <code>@ConversationScoped</code> instead of <code>@ViewScoped</code>.</p> </blockquote> <p>Then</p> <pre><code>@javax.inject.Named @javax.faces.bean.[Cdi]Scoped </code></pre> <p>can be used for CDI beans directly referenced from your JSF pages AFAIK. I haven't had any problems with the above combinations so far, so you could consider <code>@ManagedBean</code> obsolete here.</p> <p>What's left is the service layer, here mostly transactional EJB service beans declared as</p> <pre><code>@javax.ejb.* </code></pre> <p>mostly @javax.ejb.Stateless. You can even annotate and use <a href="http://www.adam-bien.com/roller/abien/entry/ejb_3_1_killed_the" rel="nofollow noreferrer">EJBs directly from JSF pages</a> - though I'm not sure if this design is desirable. To reference (inject) any components annotated with @javax.ejb.*, e.g. <code>@Stateless</code>, prefer <code>@Inject</code> over <code>@EJB</code> <a href="http://www.germanescobar.net/2010/04/4-areas-of-possible-confusion-in-jee6.html" rel="nofollow noreferrer">as described here</a>. (Probably an ancestor of this answer...)</p> <p>Finally, a very nice overview of Java EE 6 annotations can be found here: <a href="http://www.physics.usyd.edu.au/~rennie/javaEEReferenceSheet.html" rel="nofollow noreferrer">http://www.physics.usyd.edu.au/~rennie/javaEEReferenceSheet.html</a></p> <p><strong>Note</strong>: the above info is not from an expert, but simply my own take/sight from a newcomers perspective on this <strong>ridiculously confusing Java EE 6 annotations spaghetti</strong>. More insight has yet to be developed. I hope this answer can endure to be a general, practical answer to this confusion - even though it has gone a little overboard in the context of the original question.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload