How to call a action on page load in jsf

Hi all,

i am having a JSF page where i want to display the records from the datatable. I have a method in bean which call database and gets the list of records. If i call this method from a link (action on this link) present on this page i can display the records on this page.

But i want to display these records at the time of page load only. How we can do this? How to call a action at the time of page load?

Thanks in adv.

[451 byte] By [RavindraKshirsagara] at [2007-11-27 1:29:17]
# 1
Call it in the constructor or initialization block of the backing bean. Or even in the getter of the list of the datatable. Also see http://balusc.xs4all.nl/srv/dev-jep-dat.html for some insights.
BalusCa at 2007-7-12 0:28:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

If i am calling the database class and get the list in constructor only i am able to display list in the datatable on page load. This works fine. But the requirment is that if there are no records in the list display a error page with message-No records found! . But how i can navigate from the constructor to error page?

Like-

class MyBeam{

MyBean{

private List requestVOList = null;

MyDao dao = new MyDao() //for fetching list from DB

List records = dao.getList() //get list from dao class

if(records==null)

{

//Navigate to error page. How to do this navigation here?

}

else

{

requestVOList = records;

}

//setter/getters for requestVOList

}

}

RavindraKshirsagara at 2007-7-12 0:28:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
HttpServletResponse#sendRedirect() is what you need.Another way is to just gently display the message on the same page.
BalusCa at 2007-7-12 0:28:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

I tried to redirect to errorpage but it is throwing below exception-

[/ErrorPage.jsp]: Initialization successful.

Could not invoke the service() method on servlet /Pages/ErrorPage.jsp. Exception thrown : java.lang.NullPointerException

but there is no problem with ErrorPage.jsp when i am navigating to it from a action (like doing same thing on click of the button, and if no records found display error page. This time it is perfectly displaying the same error page), but if i am calling it from the constructor it is thowing above exception.

What might be the prob?

RavindraKshirsagara at 2007-7-12 0:28:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...