1

I'm using EJB and JSF. I made a jsp simple page with a button "get list". When it's clicked, a managed bean method is called that sets the list, which is then displayed in the jsp with the dataTable tag.

The question is, how can I pre load this list/dataTable on page load without having to click the button?

This is the method that's called through the button action on the jsp:

public String retrieveList() {
    items = facade.getAllItem();
    return "";
}

this is the part of the jsp:

<h:dataTable value="#{mybean.items}" var="sup"
    binding="#{mybean.dataTable}"
    rowClasses="oddRow, evenRow"
    styleClass="tableStyle"
    headerClass="tableHeader"
    columnClasses="column1, column2, column1, column1, column1, column1">
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Jojje
  • 1,749
  • 3
  • 25
  • 44

2 Answers2

4

You can add a method init with @postConstruct

@PostConstruct
public void init(){
  items = facade.getAllItem();
}

This will return the items only on bean creation ,

Dejell
  • 13,947
  • 40
  • 146
  • 229
2

Annotate the method with @PostConstruct and get rid of return value.

@PostConstruct
public void retrieveList() {
    items = facade.getAllItem();
}

This way the method will be executed immediately after construction of the bean and injection of all @EJB dependencies. In the JSF page you just have to bind to #{bean.items} the usual way.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • You're welcome. Don't forget to mark the most helpful answer to accepted. See also http://stackoverflow.com/faq. – BalusC Sep 18 '10 at 16:15