JATO Tutorial

Chapter 2, Task 3

Task 3—Create the Customer page

Let's create a page to display the data from our CustomersModel

Step 1: Create the Customer JSP

  1. Copy the template file __page__.jsp from the /templates/1_2/ModuleTemplates directory to your /jatotutorial/module1 directory. Please note that we are now working with the documents directory, not the Java packages directory in the /WEB-INF/classes directory
  2. /JatoTutorial/jatotutorial/module1/__page__.jsp

  3. Rename the file Customer.jsp
  4. Edit the file and replace the following tokens as indicated:

    Token Replacement Text
    __appPackage__ jatotutorial
    __modulePackage__ module1
    __page__ Customer

  5. As indicated below, add the JATO JSP tags for the CustomerId, CompanyName, ContactName, Phone, and the update button. Note, some parts of the template code have been omitted here. Any omitted sections are not pertinent to this example:
<%@page info="Customer" language="java"%>
<%@taglib uri="/WEB-INF/jato.tld" prefix="jato"%>
<jato:useViewBean className="jatotutorial.module1.CustomerViewBean">

... <head> <title>JATO Tutorial Application</title> </head> <body bgcolor="#FFFFFF" text="#000000"> <jato:form name="Customer" method="post"> Customer ID: <jato:text name="CustomerID"/> <p> Company Name: <jato:textField name="CompanyName"/> <p> Contact Name: <jato:textField name="ContactName"/> <p> Phone: <jato:textField name="Phone"/> <p> <jato:button name="update"/> </jato:form> </body> </jato:useViewBean> </html>

Step 2: Create the Customer ViewBean

  1. Copy the template file __page__ViewBean.java from the /templates/1_2/ModuleTemplates directory to your /WEB-INF/classes/jatotutorial/module1 directory.
  2. /JatoTutorial/WEB-INF/classes/jatotutorial/module1/__page__ViewBean.java

  3. Rename the file CustomerViewBean.jsp
  4. Edit the file and replace the following tokens as indicated:

    Token Replacement Text
    __appPackage__ jatotutorial
    __modulePackage__ module1
    __page__ Customer

  5. As indicated below, create the child name constants CHILD_CUSTOMERID, CHILD_COMPANYNAME, CHILD_CONTACTNAME, CHILD_PHONE, and CHILD_UPDATE. Note that the values for these name constants must correspond exactly with the field names in the JSP we just created. Also add the code for the registerChildren(), createChild(), getWebActionModels(), getCustomersModel(), and handleUpdateRequest() methods. Note, some parts of the template code have been omitted here. Any omitted sections are not pertinent to this example:
package jatotutorial.module1;

...
	  
public class CustomerViewBean extends ViewBeanBase
{
    
    public CustomerViewBean()
    {
        super(PAGE_NAME);
        setDefaultDisplayURL(DEFAULT_DISPLAY_URL);
        registerChildren();
    }

    protected void registerChildren()
    {
        registerChild(CHILD_CUSTOMERID,StaticTextField.class);
        registerChild(CHILD_COMPANYNAME,TextField.class);
        registerChild(CHILD_CONTACTNAME,TextField.class);
        registerChild(CHILD_PHONE,TextField.class);
        registerChild(CHILD_UPDATE,Button.class);
    }
	
    protected View createChild(String name)
    {
        if (name.equals(CHILD_CUSTOMERID))
        {
            StaticTextField child = new StaticTextField(this,
                getCustomersModel(),
                CustomersModel.FIELD_CUSTOMERID,
                "");
            return child;
        }
        else
        if (name.equals(CHILD_COMPANYNAME))
        {
            TextField child = new TextField(this,
                getCustomersModel(),
                CustomersModel.FIELD_COMPANYNAME,
                "");
            return child;
        }
        else
        if (name.equals(CHILD_CONTACTNAME))
        {
            TextField child = new TextField(this,
                getCustomersModel(),
                CustomersModel.FIELD_CONTACTNAME,
                "");
            return child;
        }
        if (name.equals(CHILD_PHONE))
        {
            TextField child = new TextField(this,
                getCustomersModel(),
                CustomersModel.FIELD_PHONE,
                "");
            return child;
        }
        else
        if (name.equals(CHILD_UPDATE))
        {
            Button child = new Button(
                this,
                CHILD_UPDATE,
                "Update");
            return child;
        }
        else
        {
            throw new IllegalArgumentException(
                "Invalid child name ["+name+"]");
        }
    }

    public Model[] getWebActionModels(int executionType)
    {
        List modelList=new ArrayList();
        switch (executionType)
        {
            case MODEL_TYPE_RETRIEVE:
                modelList.add(getCustomersModel());
                break;
		
            case MODEL_TYPE_UPDATE:
                modelList.add(getCustomersModel());
                break;
		
            case MODEL_TYPE_DELETE:
                break;
		
            case MODEL_TYPE_INSERT:
                break;
		
            case MODEL_TYPE_EXECUTE:
                break;
        }

        return (Model[])modelList.toArray(new Model[modelList.size()]);
    }


    public CustomersModel getCustomersModel()
    {
        if (customersModel==null)
            customersModel=(CustomersModel)getModel(CustomersModel.class);
        return customersModel;
    }


    public void handleUpdateRequest(RequestInvocationEvent event)
        throws ServletException, IOException 
    {
        handleWebAction(WebActions.ACTION_UPDATE);
        forwardTo();
    }


    public static final String PAGE_NAME="Customer";
    public static final String DEFAULT_DISPLAY_URL=
        "/jatotutorial/module1/Customer.jsp";

    public static final String CHILD_CUSTOMERID="CustomerID";
    public static final String CHILD_COMPANYNAME="CompanyName";
    public static final String CHILD_CONTACTNAME="ContactName";
    public static final String CHILD_PHONE="Phone";
    public static final String CHILD_UPDATE="update";

    private CustomersModel customersModel=null;
}