1

I have a @ManagedBean in my app, with the @sessionScoped anotation.

The problem is that the public no arg constructor is being called for each request, so my fields are being reseted and my model logic goes to space.

I tried build and clean, reset glassFish, but still the bean constructor get's called at each request. I also have a faces-config.xml to control the page navigation.

I could solve this by redeclaring the bean inside the faces-config, now it work's...

Any ideas of why that's happening? If i delete the faces-config, the problem persists.

Thanks everybody!

Fernando
  • 7,785
  • 6
  • 49
  • 81
  • 1
    @Shahzeb: OP is definitely using JSF2. Otherwise the `@ManagedBean` wouldn't be importable on the class nor be constructed on each request at all. – BalusC Nov 09 '11 at 22:50

1 Answers1

5

That can happen if @SessionScoped is not of the javax.faces.bean package, but instead of a completely different API, for example the javax.enterprise.context package. Doublecheck your imports and be careful with IDE autocompletion. IDEs tend to sort autosuggestions by package name and thus the javax.enterprise.context would appear as 1st option.

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class Bean {
    // ...
}

If you aren't using any javax.faces.bean scope annotation on a @ManagedBean, then the bean will default to @NoneScoped, which means that it's constructed on every single #{bean} EL evaluation.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555