tags:

views:

25

answers:

1

I am just getting started using JavaServer Faces and am having a bit of trouble wrapping my mind around how parts of the navigation work.

What I want to do is have my application hit a java method before loading the welcome page in order for that data to be available on the welcome page. I know how to do this on other pages by creating using the following:

<h:commandLink action="#{myController.methodName}" />

And then having that method return an outcome that would then go to the page that I want. However, I am unsure how to do this for the welcome page.

+1  A: 

Just put the desired code in the constructor of the managed bean class associated with the page.

public Bean() {
    // Do your stuff here.
}

Alternatively, you can declare a bean method with the @PostConstruct annotation. Such a method will be executed directly after construction and initialization/setting of all managed properties.

@PostConstruct
public void init() {
    // Do your stuff here.
}

This is more useful if the action depends on request parameters and/or another beans.

BalusC
How do I associate a managed bean with the page? Sorry if this is an obvious answer, I'm new to JSF, I'm used to using Servlets and JSP.Thanks for the help!
Jared
Just have `#{bean.property}` somewhere in the page. You want to display some data from it, is it? On `#{bean}` JSF will construct the bean automagically if it's not constructed yet.
BalusC
That works perfectly. Thanks for your help!
Jared
You're welcome.
BalusC