Hi all, I want to, write some java code that must work while the page is loaded. For example, I want to populate the datatable in the page while the page is opening. I tried the setForm method of the page's backing bean but didnt work. The same code works if I put it under the same page's command button action. Then, I tried the constructor. The bean is in the request scope. But in the constructor, I cant access the beans other objects. For example, there is a dataTable in the page and I want to populate it in the constructor. But when I access the dataTable object in the constructor, it gives nullPointerException in the runtime. (for ex: dataTable2.setBorder(1); throws exception in the constructor). The same code works if i put it under a command buttons action in the sam page..
I think, there must be a solution for initialization the page objects. Thanks...
Welcome to the JavaRanch. We don't have many rule around here, but we do ask that you make your display name conform to our Naming Standards. Please read them and adjust your display name accordingly. It helps us look more professional - even if we do have a moth-eaten mooose head as our logo.
There's limits to what you can overlap in a web app, whether you use JSF, raw servlets, or PERL CGI. One of those limits is that HTTP is a request/response protocol and not a client-server one, so the only way to get any overlap at all would be to spin off a worker thread that could be picked up on the next client request. An HTTP request isn't complete until the server-side code completes.
Also, as a general rule, backing beans in JSF should not talk directly to JSF objects. That is, you wouldn't normally make a bean that seeks out a datatable and stuff things into it. Instead you define the databable to reference the backing bean and have the datatable load itself from the backing bean - which it does automatically when you set the datatable's value attribute. This is a principle known as Inversion of Control (or IoC, for short). [ March 21, 2006: Message edited by: Tim Holloway ]
Sometimes the only way things ever got fixed is because people became uncomfortable.
Hi Tim, I have a question that is also related to page initialization.
I have a web site that is database driven. When a user logs in, their profile is looked up in a database and is used to determine the next page to be displayed. For example, an administrator might be taken to a page that allows them to run special admin reports. If a user is registering for a conference, the registration is customized based on the conference being registered for. If the user is registering for conference X, conference details are retrieved from the database before the registration page opens. When the user sees the registration page it might say: National Association Of Space Engineers New Ideas Conference May 3 - 7 2006 Registration begins April 1, 2006
I understand how a backing bean can be used to display these values, but when and how would these values be obtained?
You can do this with a PhaseListener. Just regiser a new listener for the RESTORE_VIEW phase, and implement the beforePhase method; you can do this in the backing bean's constructor, or somewhere elase. This is pretty much the same as the Page_Load event handler in .NET. So far I have seen only examples which declare listeners in faces-config.xml for the whole application (not per page).
Thanks for all the help! I got the phase listener working. After the user logs in, they are displayed an instructions page (instructions.jsp) with a backing bean (InstructionPageBean.java). I have the backing bean coded to go out to the database and fetch the name of the image file to be displayed. Now my problem:
After logging in, the instructions page opens, but without displaying the image file.
If I click on the browser's refresh button, the image displays correctly. What am I doing wrong? Is InstructionPageBean's constructor getting called after the view is rendered for instrucions.jsp? Any help would be appreciated.
Here is the relevant code:
faces_config.xml - UserBean is a backing bean for the login screen (login.jsp). InstructionPageBean is a backing bean for instructions.jsp.
[ March 30, 2006: Message edited by: Steven Gray ] [ March 30, 2006: Message edited by: Steven Gray ]
Remember to always leap before you look. But always take the time to smell the tiny ads:
Free, earth friendly heat - from the CodeRanch trailboss