Exercise 1.4: Creating new records
Before you begin, complete Exercise 1.3: Testing the Web site.
In this exercise, you will create a page that allows users to create new classified ads and post them to the database. First you will create a relational record to represent a new record in the database, and then you will create a visualization of the record on your page as a set of input fields.
Create the relational record
- Double-click the new_record.jsp file in the Project Explorer view to open it.
- Delete the default text Place content here.
- In the Palette view, click the Data drawer to expand it.
- Drag the Relational Record component from the Palette onto the blank content area. The Add Relational Record window opens.
If a warning message appears and says that a connection to your database could not be established, then you left the server running after testing the Web site. If this happens, click Cancel in each dialog and stop the server as explained in Stopping the server in Exercise 1.3: Testing the Web site.
- In the Name field, type create_record.
- Under Create controls for, click Creating a new record.
- Make sure Add input/output controls to display the record on the web page is selected.
The Add Relational Record window should look like this:
- Click Next.
- In the Table box, click the W5SAMPLE.ADS table.
- Click Next.
- Click Next again. The Configure Data Controls page opens.
Customizing the relational record visualization
The Configure Data Controls page helps you customize the visualization of your relational record. For example, you can change the columns, field labels, and submit button on your input form. After you finish these steps, a fully functional input form will be on the page.
- In the Fields to display section, clear the check box next to every field name except for the ones you want in your input form:
- ID
- TITLE
- DESCRIPTION
- MAINCATEGORY
- PRICE
- PHONE.
- Rearrange the fields into the following order from top to bottom:
To change the order of the fields, click a field name and then click the Up
- ID
- TITLE
- DESCRIPTION
- MAINCATEGORY
- PRICE
- PHONE
or Down
arrows.
- Rename the labels as you like. For example, shorten the "Maincategory:" label to just "Category:".
To rename the labels generated for the input fields, click on a label from the Label column. The mouse icon turns into a cursor so you can type new text.
- Click Options. The Options window opens.
- Make sure the Submit button option is selected.
- Type Post New Listing in the Label field.
- Click OK.
The Add Relational Record window should now look like this:
- Click Finish to generate the input form. It should look like this:
The form has an Error Messages field. This does not mean that your project has errors; this field marks the place where errors will be displayed if there are any when the user submits the form.
Programming the Submit button
When your input form is submitted, the page will automatically add the new record to the database. You can program the Post New Listing button to return to the all_records.jsp page so that you can immediately view the new record in the database. To do this,...
- In the new_record.jsp file, click the Post New Listing button you created in the form on the Web page.
- Open the Quick Edit view.
The Quick Edit view is typically at the bottom center of the workbench, in the tabs next to the Properties view. If you can't find the Quick Edit view, go to the menu bar and click Window > Show View > Quick Edit.
The Quick Edit view is a context-sensitive Java editor that can help you code events for your Faces components. The Quick Edit view now contains the generated code that handles creating a new record in the database, as shown below:
- In the Quick Edit view, click to place the cursor directly before the code return "";
- Press Enter. A new line appears before return "";
- Right-click on the new blank line, and then choose Insert Snippet > Go To Page from the context menu. The Edit GotoPage Action window opens.
- From the drop-down list, select all_records.jsp as the target page.
- Click OK.
- The event code for your button should look like this:
- Save the file and then run the page on your test server if you'd like. While running the page on the test server, you can add new records and return to the all_records.jsp page to view them.
Preventing duplicate keys
Since the ID column is a primary key in the ADS table, you cannot add records with an ID value that already exists in the table. In Module 2, you will see how to use automatic key generation to automatically create a new unused key for each new record.
Until then, enter an unused ID number in this page to add a new record. The records that come with the database use ID numbers from 1 to 22, so you can enter any number above 22 as a primary key. Be sure not to duplicate keys if you enter more than one record.
Binding input to the relational record (optional)
The input form on the page is a set of JavaServer Faces input fields which have been bound to the relational record that you created. Recall that you created the relational record to represent a new record in the ADS table of your database. Binding is a method by which you can link a JavaServer Faces input component to a column in the relational record.
When you created the relational record, the wizard automatically bound all of its columns to the input fields on the page. If you want to make changes manually, you can bind other columns to other input fields. In order to bind a column from your relational record to the input field, drag the column from the Page Data view onto the field. You can experiment with this process by deleting and recreating the Description input field on your form. Learn more about the Page Data view.
The following steps are provided to illustrate the concept of the input field and the process of binding; however, walking through these steps is optional in this tutorial. If you don't want to do this, move on to Exercise 1.5: Creating an update page.
- In the new_record.jsp file, click the Description input field.
- Press the Delete key.
- In the Palette view, click on the Faces Components drawer to expand it.
- Drag an Input component from the Palette onto the cell that contained the Description input field you just deleted.
There is now an input field in this cell, but there is no text such as {ID} or {TITLE} inside it because this input field is not bound to any column.
- In the Page Data view, drag the DESCRIPTION column onto the Input component you just created. The text inside the Input component changes to indicate that it is now bound to the DESCRIPTION column, as in this picture:
- Save the file and then run the page on the test server if you would like to.
Now you can create and modify your own JavaServer Faces input forms. You are ready to begin Exercise 1.5: Creating an update page.