About a Page's Lifecycle and Some Tutorials Announcements
By divas on Feb 10, 2006
When you create a page from Creator's IDE, the IDE inserts several lifecycle methods into the Java source. These methods have Javadoc comments that explain when to use them. Unfortunately, the comments require an indepth knowledge of the JSF lifecycle.
A writer is working on an article about Creator's application model. In the meantime, here is a quick and dirty explanation of what to put in the lifecycle methods.
init. Use this method to initialize resources, such as constants and bean properties. Do not use this method to get or set component values (such as getText() and setText()). When init is called, the page's components may have values, but these values can be stale. If the page is being displayed after submission, the values will not reflect the submitted values. Therefore, the init method is not a good place to reference component values. Nor is it a good place to set component properties. This is because the values can get overwritten after the init method completes.
preprocess. If doing a postback, this method gets called after init. A postback happens when a page is submitted, such as by clicking a button or changing a value in a component for which Auto-Submit on Change is true. This method is where you can initialize stuff needed for conversions, validations, and action handling (such as a button action method). David Botteril shows a good preprocess example in his blog about conditional validation in Java Studio Creator 2. Just as with init, do not get component values in this method as they can be stale, and do not set the values because your setting can get overwritten.
prerender. The prerender method is called after conversions, validations, and action event handling happens, and after the components have been updated with all the submitted values that passed conversions and validations. Now it is finally safe to get and set component values. This method only gets called if the page is actually going to be rendered. If it is a postback where, after the form submission, the application goes to another page, this method doesn't get called. Note: The action event handlers are another place where it is safe to get and set component values.
destroy. This is called after the page is rendered. You can use it to free up resources that were necessary just for the rendering of the page. If your page uses data providers, the IDE sticks code to close the data providers in the destroy method. When a page submission redirects to a different page, there is no guarantee which page's destroy method gets called first. This is not a good place to store away values that are based on user input into request, session, or application beans. The action event methods are better places for doing that.
New and Updated Tutorials Next Week
Hopefully, by the middle of the week, you will see the following new tutorials:
- Delving Into Components
- Using the Page Alert Component
- Working With Data Providers
- Deploying to Sun Java System Application Server
- Deploying to a BEA WebLogic Server
We also updated the following tutorials based on user feedback. These should get posted sometime next week as well.
- Using Tree Components
- Using Message Components
- Using Databound Components to Access Databases
- Using Page Fragments
We are working on tutorials about using the Calendar component about about creating a theme. If you are anxious to learn about creating themes, check out Gregory Murphy's Rough Guide: Editing Themes for Creator 2.0. We are also working on a hands-on lab proposal for JavaOne about using AJAX technology in Creator applications.