@SessionScoped bean looses scope and gets recreated all the time, fields become null

南楼画角 提交于 2019-11-26 14:46:08

You're using the wrong @SessionScoped annotation.

If you've registered the bean with the JSF @ManagedBean annotation, then you need to import the @SessionScoped from the JSF (javax.faces) package as follows:

import javax.faces.bean.SessionScoped;

When you incorrectly use a CDI scope on a JSF managed bean, then there is effectively no JSF scope for the JSF managed bean and it falls back to its default @RequestScoped, which creates a new instance in each HTTP request.

If you've registered the bean with the CDI @Named annotation, then you need to import the @SessionScoped from the CDI (javax.enterprise.context) package as follows:

import javax.enterprise.context.SessionScoped;

When you incorrectly use a JSF scope on a CDI managed bean, then there is effectively no CDI scope for the CDI managed bean and it falls back to its default @Dependent scope, which creates a new instance in each EL expression.

See also:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!