Annotation Type ViewScoped
-
@NormalScope(passivating=true) @Inherited @Documented @Target({TYPE,FIELD,METHOD}) @Retention(RUNTIME) public @interface ViewScoped
When this annotation, along with
jakarta.inject.Named
is found on a class, the runtime must place the bean in a CDI scope such that it remains active as long asNavigationHandler.handleNavigation(jakarta.faces.context.FacesContext, java.lang.String, java.lang.String)
does not cause a navigation to a view with a viewId that is different than the viewId of the current view. Any injections and notifications required by CDI and the Jakarta EE platform must occur as usual at the expected time.If
ProjectStage
is notProjectStage.Production
, verify that the currentUIViewRoot
does not have itstransient
property set totrue
. If so, add aFacesMessage
for the currentviewId
to theFacesContext
stating@ViewScoped
beans cannot work if the view is marked as transient. Also log aLevel.WARNING
message to the log. IfProjectStage
isProjectStage.Production
, do not do this verification.The bean must be stored in the map returned from
UIViewRoot.getViewMap(boolean)
.Use of this annotation requires that any beans stored in view scope must be serializable and proxyable as defined in the CDI specification.
The runtime must ensure that any methods on the bean annotated with
PostConstruct
orPreDestroy
are called when the scope begins and ends, respectively. Two circumstances can cause the scope to end.-
FacesContext.setViewRoot(jakarta.faces.component.UIViewRoot)
is called with the newUIViewRoot
being different than the current one. -
The session, that happened to be active when the bean was created, expires. If no session existed when the bean was created, then this circumstance does not apply.
In the session expiration case, the runtime must ensure that
FacesContext.getCurrentInstance()
returns a valid instance if it is called during the processing of the@PreDestroy
annotated method. The set of methods onFacesContext
that are valid to call in this circumstance is identical to those documented as "valid to call this method during application startup or shutdown". On theExternalContext
returned from thatFacesContext
, all of the methods documented as "valid to call this method during application startup or shutdown" are valid to call. In addition, the methodExternalContext.getSessionMap()
is also valid to call.- Since:
- 2.2
-