Wiki

Clone wiki

Oracle JSF Expert 1Z0-896 / JSF Scopes

#JSF Scopes

javax.faces.bean Annotations

The following scope related annotations exist in this package

  • @RequestScoped this is the default scope if one isn't specified
  • @ApplicationScoped the scope of these is the same as the application
  • @SessionScoped
  • @ViewScoped lives as long as a "view" stays alive, useful for pages that process AJAX requests.
  • @NoneScoped a none scoped bean will be initialised every time it is access via an EL expression, an exception to this is when a none scoped bean is for example injected into a @SessionScoped bean using @ManagedProperty, in that case it will live as long as the session.
  • @CustomScoped

Example

import javax.faces.bean.ManagedBean
import javax.faces.bean.RequestScoped

@ManagedBean
@RequestScoped
public class UserBean {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">
<h:head>
    <title>User Bean</title>
</h:head>
<h:body>
    <h:form>
        <h:panelGrid columns="2">
            <h:inputText id="name" value="#{userBean.name}" required="true">
                <f:ajax event="keyup" render="error" />
            </h:inputText>
            <h:message for="name" id="error" style="color: red" />
        </h:panelGrid>
    </h:form>
</h:body>
</html>

CDI Scopes

javax.enterprise.context annotations

  • @RequestScoped
  • @SessionScoped
  • @ApplicationScoped
  • @ConversationScoped

Updated