I have index.html page with
<h:dataTable id="usersTable" value="#{mainViewController.users}" var="user" border="1">
....
and request scoped mainViewController bean
@Component("mainViewController")
@Scope("request")
public class MainViewController {
@Inject
private UserDao userDao;
private Collection<User> users;
public Collection<User> getUsers() {
if (users == null) {
users = userDao.findAll();
}
return users;
}
when I access index.html getUsers is called, that is absolutely normal, but when I leave index.html to some other page getUsers is also called, how avoid secondary call?
Don’t use POST for page-to-page navigation. So don’t use a
<h:commandLink>or<h:commandButton>to navigate to another page. It will unnecessarily submit the form to the server and recreate the same bean. Just use<a>,<h:outputLink>,<h:link>or<h:button>for page-to-page navigation. They fire a GET request straight on the target URL.Another advantage of using GET for page-to-page navigation is that Searchbots will index the pages. Thus, better for SEO.