Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h1>Introduction</h1> <p>It represents the scope (the lifetime) of the bean. This is easier to understand if you are familiar with "under the covers" working of a basic servlet web application: <a href="https://stackoverflow.com/questions/3106452/how-do-servlets-work-instantiation-session-variables-and-multithreading">How do servlets work? Instantiation, sessions, shared variables and multithreading</a>.</p> <hr> <h1><code>@Request/View/Flow/Session/ApplicationScoped</code></h1> <p>A <a href="https://docs.oracle.com/javaee/7/api/javax/enterprise/context/RequestScoped.html" rel="noreferrer"><code>@RequestScoped</code></a> bean lives as long as a single HTTP request-response cycle (note that an Ajax request counts as a single HTTP request too). A <a href="https://docs.oracle.com/javaee/7/api/javax/faces/view/ViewScoped.html" rel="noreferrer"><code>@ViewScoped</code></a> bean lives as long as you're interacting with the same JSF view by postbacks which call action methods returning <code>null</code>/<code>void</code> without any navigation/redirect. A <a href="http://docs.oracle.com/javaee/7/api/javax/faces/flow/FlowScoped.html" rel="noreferrer"><code>@FlowScoped</code></a> bean lives as long as you're navigating through the specified collection of views registered in the flow configuration file. A <a href="https://docs.oracle.com/javaee/7/api/javax/enterprise/context/SessionScoped.html" rel="noreferrer"><code>@SessionScoped</code></a> bean lives as long as the established HTTP session. An <a href="https://docs.oracle.com/javaee/7/api/javax/enterprise/context/ApplicationScoped.html" rel="noreferrer"><code>@ApplicationScoped</code></a> bean lives as long as the web application runs. Note that the CDI <code>@Model</code> is basically a <a href="https://docs.oracle.com/javaee/7/api/javax/enterprise/inject/Model.html" rel="noreferrer">stereotype</a> for <code>@Named @RequestScoped</code>, so same rules apply.</p> <p>Which scope to choose depends solely on the data (the state) the bean holds and represents. Use <code>@RequestScoped</code> for simple and non-ajax forms/presentations. Use <code>@ViewScoped</code> for rich ajax-enabled dynamic views (ajaxbased validation, rendering, dialogs, etc). Use <code>@FlowScoped</code> for the "wizard" ("questionnaire") pattern of collecting input data spread over multiple pages. Use <code>@SessionScoped</code> for client specific data, such as the logged-in user and user preferences (language, etc). Use <code>@ApplicationScoped</code> for application wide data/constants, such as dropdown lists which are the same for everyone, or managed beans without any instance variables and having only methods.</p> <p>Abusing an <code>@ApplicationScoped</code> bean for session/view/request scoped data would make it to be shared among all users, so anyone else can see each other's data which is just plain wrong. Abusing a <code>@SessionScoped</code> bean for view/request scoped data would make it to be shared among all tabs/windows in a single browser session, so the enduser may experience inconsitenties when interacting with every view after switching between tabs which is bad for user experience. Abusing a <code>@RequestScoped</code> bean for view scoped data would make view scoped data to be reinitialized to default on every single (ajax) postback, causing possibly non-working forms (<a href="https://stackoverflow.com/a/2120183/157882">see also points 4 and 5 here</a>). Abusing a <code>@ViewScoped</code> bean for request, session or application scoped data, and abusing a <code>@SessionScoped</code> bean for application scoped data doesn't affect the client, but it unnecessarily occupies server memory and is plain inefficient.</p> <p>Note that the scope should rather not be chosen based on performance implications, unless you <em>really</em> have a low memory footprint and want to go completely stateless; you'd need to use exclusively <code>@RequestScoped</code> beans and fiddle with request parameters to maintain the client's state. Also note that when you have a single JSF page with differently scoped data, then it's perfectly valid to put them in separate backing beans in a scope matching the data's scope. The beans can just access each other via <code>@ManagedProperty</code> in case of JSF managed beans or <code>@Inject</code> in case of CDI managed beans.</p> <h3>See also:</h3> <ul> <li><a href="https://stackoverflow.com/questions/6025998/difference-between-view-and-request-scope-in-managed-beans/">Difference between View and Request scope in managed beans</a></li> <li><a href="https://stackoverflow.com/questions/28240734/advantages-of-using-jsf-faces-flow-instead-of-the-normal-navigation-system/">Advantages of using JSF Faces Flow instead of the normal navigation system</a></li> <li><a href="http://balusc.blogspot.com/2011/09/communication-in-jsf-20.html#ManagedBeanScopes" rel="noreferrer">Communication in JSF2 - Managed bean scopes</a></li> </ul> <hr> <h1><code>@CustomScoped/NoneScoped/Dependent</code></h1> <p>It's not mentioned in your question, but (legacy) JSF also supports <a href="https://docs.oracle.com/javaee/7/api/javax/faces/bean/CustomScoped.html" rel="noreferrer"><code>@CustomScoped</code></a> and <a href="https://docs.oracle.com/javaee/7/api/javax/faces/bean/NoneScoped.html" rel="noreferrer"><code>@NoneScoped</code></a>, which are rarely used in real world. The <code>@CustomScoped</code> must refer a custom <code>Map&lt;K, Bean&gt;</code> implementation in some broader scope which has overridden <code>Map#put()</code> and/or <code>Map#get()</code> in order to have more fine grained control over bean creation and/or destroy. </p> <p>The JSF <code>@NoneScoped</code> and CDI <a href="http://docs.oracle.com/javaee/7/api/javax/enterprise/context/Dependent.html" rel="noreferrer"><code>@Dependent</code></a> basically lives as long as a single EL-evaluation on the bean. Imagine a login form with two input fields referring a bean property and a command button referring a bean action, thus with in total three EL expressions, then effectively three instances will be created. One with the username set, one with the password set and one on which the action is invoked. You normally want to use this scope only on beans which should live as long as the bean where it's being injected. So if a <code>@NoneScoped</code> or <code>@Dependent</code> is injected in a <code>@SessionScoped</code>, then it will live as long as the <code>@SessionScoped</code> bean.</p> <h3>See also:</h3> <ul> <li><a href="https://stackoverflow.com/questions/30748724/expire-specific-managed-bean-instance-after-time-interval/">Expire specific managed bean instance after time interval</a></li> <li><a href="https://stackoverflow.com/questions/3086896/what-is-none-scope-bean-and-when-to-use-it/">what is none scope bean and when to use it?</a></li> <li><a href="https://stackoverflow.com/questions/19322364/what-is-the-default-managed-bean-scope-in-a-jsf-2-application-in-netbeans/">What is the default Managed Bean Scope in a JSF 2 application?</a></li> </ul> <hr> <h1>Flash scope</h1> <p>As last, JSF also supports the flash scope. It is backed by a short living cookie which is associated with a data entry in the session scope. Before the redirect, a cookie will be set on the HTTP response with a value which is uniquely associated with the data entry in the session scope. After the redirect, the presence of the flash scope cookie will be checked and the data entry associated with the cookie will be removed from the session scope and be put in the request scope of the redirected request. Finally the cookie will be removed from the HTTP response. This way the redirected request has access to request scoped data which was been prepared in the initial request.</p> <p>This is actually not available as a managed bean scope, i.e. there's no such thing as <code>@FlashScoped</code>. The flash scope is only available as a map via <a href="http://docs.oracle.com/javaee/7/api/javax/faces/context/ExternalContext.html#getFlash--" rel="noreferrer"><code>ExternalContext#getFlash()</code></a> in managed beans and <code>#{flash}</code> in EL.</p> <h3>See also:</h3> <ul> <li><a href="https://stackoverflow.com/questions/13685633/how-to-show-faces-message-in-the-redirected-page/">How to show faces message in the redirected page</a></li> <li><a href="https://stackoverflow.com/questions/25694423/pass-an-object-between-viewscoped-beans-without-using-get-params">Pass an object between @ViewScoped beans without using GET params</a></li> <li><a href="https://stackoverflow.com/questions/7812985/cdi-missing-viewscoped-and-flashscoped/">CDI missing @ViewScoped and @FlashScoped</a></li> </ul>
    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.
    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