Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In a Java EE / JSF web application, there are basically 2 APIs available to manage your beans.</p> <p>Those, as in your initial question,</p> <pre><code>import javax.enterprise.context.RequestScoped; import javax.inject.Named; @Named("test") @RequestScoped </code></pre> <p>are from <a href="https://stackoverflow.com/tags/cdi/info">CDI</a> (Context and Dependency Injection) which is a Java EE standardized way of managing beans (and designed to standardize/supplant among others Spring DI and AOP). This is only available in containers supporting Java EE web profile such as Glassfish, JBoss AS, etc, but not in barebones servlet containers such as Tomcat and Jetty (you <a href="http://docs.jboss.org/weld/reference/1.0.0/en-US/html/environments.html#d0e4910" rel="nofollow noreferrer">can</a> however install it separately; note, Weld is the reference implementation of CDI, like as Mojarra is the reference implementation of JSF). </p> <p>In order to get CDI to run, you need to have a physical <code>/WEB-INF/beans.xml</code> file in your webapp. The file itself can just be kept empty. Your initial problem suggests that you didn't have one. When this file is absent, then CDI won't be initialized for the webapp, speeding up the server startup time.</p> <p>Another way, as you figured in your own answer,</p> <pre><code>import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; @ManagedBean("test") @RequestScoped </code></pre> <p>are from <a href="https://stackoverflow.com/tags/jsf/info">JSF</a> itself. This is particularly helpful if you intend to keep your web application portable across various containers, including barebones JSP/Servlet containers such as Tomcat and Jetty. They do not require any additional configuration files in order to get to run.</p> <p>Noted should be that <code>eager=true</code> is unnecessary on a request scoped bean. Even more, it would be ignored anyway. It's only accepted on an application scoped bean. See also e.g. <a href="https://stackoverflow.com/questions/13378221/how-can-i-initialize-a-java-facesservlet">How can I initialize a Java FacesServlet</a>.</p>
 

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