开发者

JSF preload list for datatable in page [duplicate]

This question already has an answer here: How and when should I load the model from database for JSF dataTable (1 answer) Closed 7 years ago.

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, c开发者_开发问答olumn2, column1, column1, column1, column1">


You can add a method init with @postConstruct

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

This will return the items only on bean creation ,


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜