1

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?

michael nesterenko
  • 14,222
  • 25
  • 114
  • 182

1 Answers1

4

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.

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