How to prevent @PostConstruct call for postback

When the page loads for the first time, @PostConstruct is @PostConstruct , but when I @PostConstruct back on this page, @PostConstruct is called @PostConstruct .

How can I make it run only on the first request, and not on each postback?

 @PostContruct public void init() { // charge combos.... } public void submit() { // action } 
+4
source share
2 answers

Apparently your bean is the scope of the request and thus is restored with every HTTP request. I’m not quite sure why you want to prevent @PostConstruct from being called @PostConstruct , otherwise you would get an "empty" bean state that could lead to form @PostConstruct errors, but everything is fine, you can add a check if the current request not a postback .

 public void init() { if (!FacesContext.getCurrentInstance().isPostback()) { // charge combos.... } } 

Thus, part of the “assembly combos” will not be called in reverse gears.

Or maybe your actual question is not, “How to prevent post-construction from calling postback?”, But even more, “How to keep the same bean instance in postback?”. In this case, you will need to place the bean in the viewport, not in the request area.

 @ManagedBean @ViewScoped public class Bean implements Serializable { // ... } 

As long as you return null from the action methods, this way the same bean will work as long as you interact with the same view using postbacks. This way @PostConstruct will not be called (simply because the bean has not been restored).

See also:

+6
source

use this import:

import javax.faces.view.ViewScoped; for @ViewScoped

-2
source

Source: https://habr.com/ru/post/1444367/


All Articles