JATO Tutorial

Chapter 2, Task 5

Task 5—Link the Login page to the Customer page

Let's link the Login page to the Customer page, filtering the data the Customer page displays based on the user's login.

Step 1: Edit the handleSubmitRequest method in LoginViewBean

  1. Edit the file LoginViewBean.java. As shown below, modify the logic in the handleSubmitRequest() method so that in the event of a successful login, it will display the Customer page with the customer data that corresponds to the value entered in the Login ID field. Please note, we have contrived this example so that the only legal values for the Login ID field are also CustomerID values. Therefore, you can take the Login ID value and apply it the where clause used by the CustomerModel. This will ensure that the data retrieved by the CustomerModel will correspond to the appropriate CustomerID. In real-world applications, the setting of the necessary where criteria generally requires some additional steps such as the mapping of login ID to customer ID. Note, some parts of the template code have been omitted here. Any omitted sections are not pertinent to this example:
package jatotutorial.module1;

...
  
public void handleSubmitRequest(RequestInvocationEvent event)
    throws ServletException, IOException 
{
    // Retrieve the login name & password
    String loginName = getDisplayFieldStringValue(CHILD_LOGIN);
    String password = getDisplayFieldStringValue(CHILD_PASSWORD);
              
    String theMessage = "";
              
    // Check the login name
    // Note, we don't check the password in this example
    if (loginName.equalsIgnoreCase("anton") || 
        loginName.equalsIgnoreCase("alfki") ||
        loginName.equalsIgnoreCase("bonap"))
    {
        // Instead of returning the login page, display the Customer 
        // page for the customer that matches the login ID

        // theMessage = "Congratulations, " + loginName + ...
		
        // Get a reference to the CustomerModel
        CustomersModel customers =
            (CustomersModel)getModel(CustomersModel.class);
				
        // Modify the where criteria to reflect the login ID
        customers.clearUserWhereCriteria();
        customers.addUserWhereCriterion(
            CustomersModel.FIELD_CUSTOMERID, loginName.toUpperCase());
				
        // Display the Customer page
        getViewBean(CustomerViewBean.class).forwardTo(
            event.getRequestContext());
    }
    else
    {
        theMessage = "Sorry, " + loginName + 
            ", your login or passsword was incorrect!";

        // Set the ouput status message
        getDisplayField(CHILD_MESSAGE).setValue(theMessage);
        forwardTo();
    }           
}