Exercise 2.5: Using automatic key generation
Before you begin, complete Exercise 2.4: Using navigation rules.
In the previous exercise, you set up navigation rules to make sure the user entered a unique ID number for the new classified ad. This process is frustrating because the user doesn't want to guess numbers.
Of course, the best way to give each record a unique ID number is to have the database assign a unique number to each record. In this exercise, you will set up automatic key generation to make the site give a new number to each new record in the database automatically.
Automatic key generation is a complex topic, but in short, a database can choose new keys if it has a special table reserved for key generation. This table must have a list of unused keys in one column (the incrementor column) and a list of numbers in order starting with 1 in the other column (the identity column). When the database needs a new key, it takes the key from the row with the 1 in the identity column and then gets a new key ready for the next time. Learn more about automatic key generation.
Setting up the automatic key generation
The sample database provided with this tutorial has a key generation table named KEYS. Its two columns, as described above, will supply a new ID number for each new record. In these steps, you will set up the create_record relational record to take its ID number from the KEYS table.
- Double-click the new_record.jsp page in the Project Explorer view.
- Double-click the create_record relational record in the Page Data view. The Configure Relational Record window opens.
- Click the Key generation tab.
- Click Use Auto Key Generation.
- Click the W5SAMPLE.KEYS table.
- In the Select identity column field, click KEY_ID.
- In the Select incrementor column field, click NEXT_KEY.
Since you only need one key for the ad's ID, you will leave the Keys fetched at once setting at 1. If you needed multiple keys, this setting would make the database select all of them at once.
The Configure Data Object window should look like this:
- Click Close.
Now the ID field will be automatically generated for each new record. Now remove the ID input field so the user can not enter a value.
- Place the cursor in the top row of the input form table by clicking on the Id: text.
- Click Table > Delete Row.
- Save the page.
Optionally, if you wanted to see what key is being generated for you, you could instead delete the input component for the ID number and replace it with an output component bound to the ID column of create_record. Then, the automatically generated key would appear at the top of the form but the user would not be able to change it.
Running the completed tutorial
When you are ready to publish your Web application, you will need a server that will host it so users can access the Web site through the Internet. However, to test your Web site, you can use WebSphere Application Server to simulate a server for testing purposes. To find out how to test the Web site, refer to Exercise 1.3: Testing the Web site.
Congratulations!
You have completed the Displaying dynamic information on Web pages with JavaServer Faces tutorial. Continue to this tutorial's Summary.