< Previous | Next >

Lesson 2.3: Use navigation rules

In this lesson, you will learn how to set up navigation rules to redirect a site user to specific Web pages.

The way the new_record.jsp page is set up, users must be very careful not to enter an ID number that is already in use, because each record in the database must have a unique ID number. This was explained in more detail under the note "Prevent duplicate keys" in Lesson 1.5: Program the Submit button. In this lesson, you will check to see if the ID entered is unique, and if not, send the user to an error page that describes the problem and tells the user how to fix it.

Navigation rules allow you to direct users to the error page or the all_records.jsp page after checking to see if users have entered a duplicate ID number or not. You will assign aliases to these two possible outcomes and then link those two aliases to the correct target pages. In this example, an error on the create_record.jsp page will signal the ERROR_CREATE alias, which will send users to the error page. If users fill out the create_record.jsp page correctly, it will signal the MAIN alias, which will be linked to the all_records.jsp page as usual.

Set up the rules

  1. Double-click the new_record.jsp page in the Enterprise Explorer.
  2. Click the Post New Listing button on the page.
  3. In the Properties view, click Add Rule. The Add Navigation Rule window opens. The first rule will send the user to an error page named create_error.jsp if something goes wrong when creating and committing the new record to the database.
  4. In the Page field, type error_create.jsp. This page does not exist, but for the purpose of this tutorial, you can just imagine that it exists.
  5. Click the The outcome named radio button.
  6. Type ERROR_CREATE in the text field after the The outcome named radio button.
  7. Click the This page only radio button because there is no other page in the site where the user could trigger this particular error by entering a duplicate ID number. Click OK. The next rule will navigate to all_records.jsp if the user entered a valid ID number.
  8. Click the Add Rule button to open the Add Navigation Rule window again.
  9. Use the Page drop down box to select all_records.jsp.
  10. Click the The outcome named checkbox and then type MAIN into the text field after it.
  11. Since you might want to reuse this rule in another page (for instance, the update_record.jsp page), click the All pages radio button under This rule is used for. Click OK. The two rules are now displayed on the Properties view.

Return aliases from the button action

All that remains now is to put the new navigation rules to work. You will add two return statements into the code for the Post New Listing button. These return statements summon the appropriate alias so the user is sent to the appropriate page as set in the navigation rule.
  1. Click the Post New Listing button.
  2. Open the Quick Edit view.
  3. In the Quick Edit view, find the line that reads } catch (Throwable e) {. This catch function runs if the user has entered a duplicate ID number.
  4. Remove all the code between the opening curly brace { at the end of this line and the next closing curly brace } a few lines down. Do not remove either curly brace.
  5. In place of the code between the braces, type this text:return "ERROR_CREATE";
  6. Next, remove all code below the last closing curly brace } and in its place type this text: return "MAIN";

    This step removes the gotoPage action that you added in exercise 1.4. You no longer need this code because the navigation rules do the same thing.

    Your button's code should now look like this:

    Quick edit view.

  7. Save your page and test it if you want.
Optionally, you can create a simple error page named create_error.jsp which explains to your users that an error occurred while creating their listing and to try a different ID value. You can then test these navigation rules by attempting to add a new listing that uses an existing ID value (such as 1).

Lesson checkpoint

You have completed Lesson 2.3. In this lesson you learned how to use navigation rules to create an error if user try to enter an invalid ID value, or to send the user to all_recordlist.jsp if the user enters a valid ID.
< Previous | Next >

Feedback