0

I have jsf session scoped bean. Its init method having annotation @PostConstruct is called when the page is loaded for the first time but not on every reload or refresh. I want to call some method on every page refresh. What annotation or change in xhtml file do I need to use

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Ajmal Khan
  • 87
  • 1
  • 8
  • Which annotation are you using? If you’re using a `@ViewScope` annotation it will be loaded each time the page is loaded. If you are using `@SessionScoped` the data will be persisted through all the users session. – Jacobo May 13 '18 at 20:57
  • I am using session scope, and I want method to be called for each refresh – Ajmal Khan May 14 '18 at 06:30
  • Just change it to ViewScope. – Jacobo May 14 '18 at 07:30
  • 2
    Possible duplicate of [How to choose the right bean scope?](https://stackoverflow.com/questions/7031885/how-to-choose-the-right-bean-scope) – Kukeltje May 14 '18 at 12:19

2 Answers2

1

Try change your annotation tag as follows

Just change it to ViewScope. import javax.annotation.PostConstruct;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

@Named
@ViewScoped
public class YourControllerClass implements Serializable {

    @PostConstruct
    public void init(){

    }
}
Jacobo
  • 1,259
  • 2
  • 19
  • 43
1

If you want to call a method on every page refresh it 's better to use ComponentSystemEvent.

You can refer below link https://www.tutorialspoint.com/jsf/jsf_applicationevents_tag.htm

Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47