AgileApps Support Wiki Pre Release

Lab B.3: Adding Functionality

From AgileApps Support Wiki
Revision as of 23:06, 24 August 2011 by imported>Aeric (Text replace - 'RefreshIcon.jpg' to 'RefreshIcon.png')
Goals
  • Pass a value to a controller
  • Pass a value from the controller to a JSP page
  • Use the value in the JSP page
  • Use the Debug Log
Prerequisites

Exercise

Pass a Value to the Controller

Let's start by sending your name to the controller.

  1. Click Designer > Logic > Pages
  2. Click Hello.jsp
  3. Customize the form to pass your name to the controller. (You could also create a form field for that, but here will just hardcode the value.)
    <p>Press the button to see something cool.</p>
    <form name="mainForm" action="/networking/controller/com/platform/demo/hello/HelloController?name={yourName}" 
       ...                                                                                      ~~~~~~~~~~~~~~~~
    
    For example:
    <p>Press the button to see something cool.</p>
    <form name="mainForm" action="/networking/controller/com/platform/demo/hello/HelloController?name=Fred" 
       ...
    

Customize the Controller

Next customize the controller to pass along data sent from the initial page. (You could do any processing you want here, and even select different targets, depending on the results. But for now, we'll just pass along the data.)

  1. Click Designer > Logic > Classes
  2. Click HelloController
  3. Add the code to echo the incoming data and pass it to the target page:
        public ControllerResponse execute(HashMap params) throws Exception
        {
            // Just to show we got it, and could process it if we wanted to
            String name = (String) params.get("name");
            Functions.debug("HelloController: Hal will greet "+name);
            
            // Pass the parameters to the target page
            ControllerResponse cr = new ControllerResponse();
            cr.setData(params);
            cr.setTargetPage("HelloWorld.jsp");
            return cr;
        }
    

In this code, you echoed the incoming data to the Debug Log, before passing it to the target page. In the process, you identified where the message came from (HelloController), to make it easier to find in the log.

The controller should now look like this:

package com.platform.demo.hello;

import com.platform.api.*;
import java.util.HashMap;

public class HelloController implements com.platform.api.Controller
{
    public ControllerResponse execute(HashMap params) throws Exception
    {
        // Just to show we got it, and could process it if we wanted to
        String name = (String) params.get("name");
        Functions.debug("HelloController: Hal will greet "+name);
        
        // Pass the parameters to the target page
        ControllerResponse cr = new ControllerResponse();
        cr.setData(params);
        cr.setTargetPage("HelloWorld.jsp");
        return cr;
    }
}

Customize the Destination Page

  1. Click Designer > Logic > Pages
  2. Click HelloWorld.jsp
  3. Put this code at the top of the page to process data sent by the controller, and to set a default value if none is present.
    <%
        java.util.HashMap params = new java.util.HashMap();
        String name = "World";
        if (controllerResponse != null) {
           params = (java.util.HashMap)controllerResponse.getData();
           if (params != null){
              name = (String) params.get("name");
           }
        }
    %>
    
  4. Change the output line to use the name sent by the controller:
    <h1 align="center">Hello <%=name %>!</h1>
                            ^^^^^^^^^^^^
    

HelloWorld.jsp should now look like this:

<%
    java.util.HashMap params = new java.util.HashMap();
    String name = "World";
    if (controllerResponse != null) {
       params = (java.util.HashMap)controllerResponse.getData();
       if (params != null){
          name = (String) params.get("name");
       }
    }
%>
<h1 align="center">Hello <%=name %>!</h1>

Try It Out

  1. Visit https://platform_url/networking/pages/Hello.jsp and click the button to see the results.
  2. In the platform, click Designer > Global Resources > Debug Log to view the entry you made in the log.
    If that window is already open, refresh it by click the "circled arrows" icon in the upper right: RefreshIcon.png.
You should see an entry like this:
DebugMessage.jpg

Learn More