Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In a Servlet, you can get request scoped beans by:</p> <pre><code>Bean bean = (Bean) request.getAttribute("beanName"); </code></pre> <p>and session scoped beans by:</p> <pre><code>Bean bean = (Bean) request.getSession().getAttribute("beanName"); </code></pre> <p>and application scoped beans by:</p> <pre><code>Bean bean = (Bean) getServletContext().getAttribute("beanName"); </code></pre> <hr> <p>If you're running in a dependency injection capable framework/container and the bean is managed by CDI's <a href="http://docs.oracle.com/javaee/6/api/javax/inject/Named.html" rel="noreferrer"><code>@Named</code></a> instead of JSF's <a href="http://docs.oracle.com/javaee/6/api/javax/faces/bean/ManagedBean.html" rel="noreferrer"><code>@ManagedBean</code></a>, it's even more easy:</p> <pre><code>@Inject private Bean bean; </code></pre> <hr> <p>Regardless of the scope, when you're <em>actually</em> inside the <a href="http://docs.oracle.com/javaee/6/api/javax/faces/context/FacesContext.html" rel="noreferrer"><code>FacesContext</code></a> (i.e. the current HTTP request has been served through the <a href="http://docs.oracle.com/javaee/6/api/javax/faces/webapp/FacesServlet.html" rel="noreferrer"><code>FacesServlet</code></a>), then the normal JSF2 way is using <a href="http://docs.oracle.com/javaee/6/api/javax/faces/application/Application.html#evaluateExpressionGet%28javax.faces.context.FacesContext,%20java.lang.String,%20java.lang.Class%29" rel="noreferrer"><code>Application#evaluateExpressionGet()</code></a>:</p> <pre><code>FacesContext context = FacesContext.getCurrentInstance(); Bean bean = context.getApplication().evaluateExpressionGet(context, "#{beanName}", Bean.class); </code></pre> <p>which can be convenienced as follows:</p> <pre><code>@SuppressWarnings("unchecked") public static &lt;T&gt; T findBean(String beanName) { FacesContext context = FacesContext.getCurrentInstance(); return (T) context.getApplication().evaluateExpressionGet(context, "#{" + beanName + "}", Object.class); } </code></pre> <p>and can be used as follows:</p> <pre><code>Bean bean = findBean("bean"); </code></pre> <hr> <p>However, when you're already inside a <a href="http://docs.oracle.com/javaee/6/api/javax/faces/bean/ManagedBean.html" rel="noreferrer"><code>@ManagedBean</code></a>, then using <a href="http://docs.oracle.com/javaee/6/api/javax/faces/bean/ManagedProperty.html" rel="noreferrer"><code>@ManagedProperty</code></a> is cleaner since it's more declarative.</p> <pre><code>@ManagedProperty("#{bean}") private Bean bean; </code></pre>
    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. 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