Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsf 2 Session bean created for every request [duplicate]

Tags:

jsf-2

ello

I have 2 Managed beans, one View scoped, the other Session scoped. The View scoped bean is defined as

@ManagedBean
@ViewScoped
public class InvoiceController implements Serializable {
  private static final long serialVersionUID = 1L;

  @ManagedProperty(value="#{invoiceService}")
  private InvoiceService invoiceService;

The session scoped bean as

@ManagedBean
@SessionScoped
public class InvoiceService implements Serializable{

I am using the session scoped bean to hold a flag used to decide if a panel should be rendered, when I run this through the debug I find that every time I call the method on the sesison bean, it is a new instance of the bean and therefore does not retain the value of my flag between requests.

What am I doing wrong?

like image 880
MGB Avatar asked Jun 22 '26 05:06

MGB


1 Answers

That can happen if you have imported @SessionScoped from the javax.enterprise.context package instead of from the javax.faces.bean package. The former works on CDI @Named beans only, while the latter works on JSF @ManagedBean beans only.

A JSF @ManagedBean without any valid scope would default to @NoneScoped which means that it's newly constructed on every single EL expression referencing the bean, such as the @ManagedProperty. This explains the symptoms you're seeing.

like image 93
BalusC Avatar answered Jun 27 '26 20:06

BalusC