AgileApps Support Wiki Pre Release

Difference between revisions of "Working with Pages and Classes"

From AgileApps Support Wiki
imported>Aeric
Β 
imported>Aeric
Line 38: Line 38:
The <tt>action</tt> attribute of the <tt><form></tt> tag specifies the URL of the class that you wrote that implements the <tt>Controller</tt> interface (<tt>DirectoryController</tt> in this example).
The <tt>action</tt> attribute of the <tt><form></tt> tag specifies the URL of the class that you wrote that implements the <tt>Controller</tt> interface (<tt>DirectoryController</tt> in this example).


When the user clicks the Search button, the form is submitted to <tt>DirectoryController</tt> which is shown below.
When the user clicks the Search button, the form is submitted to the DirectoryController's <tt>execute</tt> method, shown below.
Β 
===execute Method===
:<syntaxhighlight lang="java" enclose="div">
:<syntaxhighlight lang="java" enclose="div">
import com.platform.api.*;
import com.platform.api.*;
Line 71: Line 73:
</syntaxhighlight>
</syntaxhighlight>


This code:
:* Gets the value of the <tt>action</tt> key passed in <tt>params</tt>
:* Creates instances of the predefined classes <tt>[[ControllerResponse Class|ControllerResponse]]</tt> and <tt>[[Result Class|Result]]</tt>
:* Creates an instance of the worker class <tt>Directory</tt> (shown below)
:* Calls the <tt>search</tt> method (shown below) in the worker class, if the action is "search", passing it <tt>params</tt>; also sets the next JSP name
:* Puts the result of the method call in <tt>params</tt> using the key <tt>opResult</tt>
:* Calls the <tt>setData</tt> method in <tt>ControllerResponse</tt>, specifying <tt>params</tt> as the argument
:* Returns the instance of <tt>ControllerResponse</tt>


===execute Method===
===search Method===
The code in the <tt>execute</tt> method:
Here is the <tt>search</tt> method in the <tt>Directory</tt> worker class.
* Gets the value of the <tt>action</tt> key passed in <tt>params</tt>
* Creates instances of the predefined classes <tt>[[ControllerResponse Class|ControllerResponse]]</tt> and <tt>[[Result Class|Result]]</tt>
* Creates an instance of the worker class <tt>Directory</tt> (shown below)
* If the action is "search", calls the <tt>search</tt> method (shown below) in the worker class, passing it <tt>params</tt>; also sets the next JSP name
* Puts the result of the method call in <tt>params</tt> using the key <tt>opResult</tt>
* Calls the <tt>setData</tt> method in <tt>ControllerResponse</tt>, specifying <tt>params</tt> as the argument
* Returns the instance of <tt>ControllerResponse</tt>


The <tt>search</tt> method in the <tt>Directory</tt> worker class is shown below.
:<syntaxhighlight lang="java" enclose="div">
:<syntaxhighlight lang="java" enclose="div">
import com.platform.api.*;
import com.platform.api.*;
Line 114: Line 116:
</syntaxhighlight>
</syntaxhighlight>


===search Method===
This code:
The code in the <tt>search</tt> method:
:* Creates an instance of <tt>Parameters</tt> and adds <tt>params</tt> to it
* Creates an instance of <tt>Parameters</tt> and adds <tt>params</tt> to it
:* Gets the values of the <tt>searchType</tt> and <tt>searchString</tt> keys from <tt>params</tt>
* Gets the values of the <tt>searchType</tt> and <tt>searchString</tt> keys from <tt>params</tt>
:* Creates an instance of <tt>Result</tt>
* Creates an instance of <tt>Result</tt>
:* Calls <tt>searchRecords</tt>, passing in the <tt>searchType</tt> and <tt>searchString</tt> and setting the <tt>Result</tt> object with the returned value
* Calls <tt>searchRecords</tt>, passing in the <tt>searchType</tt> and <tt>searchString</tt> and setting the <tt>Result</tt> object with the returned value
:* Returns the <tt>Result</tt> object
* Returns the <tt>Result</tt> object
Β 
===Displaying the Results===
So, the search request originated in the page, went to the controller, and finally to the worker. Now we look at how the search results get displayed in the page.
So, the search request originated in the page, went to the controller, and finally to the worker. Now we look at how the search results get displayed in the page.


This examples describes:
This example:
* Gets the <tt>opResult</tt> value from <tt>params</tt> and puts it into <tt>result</tt>
:* Gets the <tt>opResult</tt> value from <tt>params</tt> and puts it into <tt>result</tt>
* Iterates through <tt>result</tt>, creating a table row for each set of data
:* Iterates through <tt>result</tt>, creating a table row for each set of data


In the page, the following code gets the search results.
In the Page, here is the code that gets the search results:


:<syntaxhighlight lang="java" enclose="div">
:<syntaxhighlight lang="java" enclose="div">
Line 176: Line 179:


This example describes how to use a [[Page]] to perform the following actions on records in the ''Directory'' object:
This example describes how to use a [[Page]] to perform the following actions on records in the ''Directory'' object:
*search for records based on specified criteria
:*search for records based on specified criteria
*add records
:*add records
*update records
:*update records


;Considerations:
;Considerations:
Line 190: Line 193:
To add the Pages and Classes:
To add the Pages and Classes:
#To create the (.java) Java pages, follow the instructions at [[Classes#Adding_a_Class|Add a Class]]
#To create the (.java) Java pages, follow the instructions at [[Classes#Adding_a_Class|Add a Class]]
#:Create the class <tt>Directory.java</tt> from: [[Directory.java|<tt>Directory.java</tt>]]
#:Create the class <tt>Directory.java</tt> from: [[Directory.java|Directory.java]]
#::*Copy and paste the contents into the Class
#::*Copy and paste the contents into the Class
#::*Save the file as <tt>Directory.java</tt>
#::*Save the file as <tt>Directory.java</tt>
Line 197: Line 200:
#::*Save the file as <tt>DirectoryController.java</tt>
#::*Save the file as <tt>DirectoryController.java</tt>
#To create the (.jsp) Java Server Pages, follow the instructions at [[Pages#Adding_a_Page|Add a Page]]
#To create the (.jsp) Java Server Pages, follow the instructions at [[Pages#Adding_a_Page|Add a Page]]
#:Create the page <tt>AddUpdate.jsp</tt> from: [[AddUpdate.jsp|<tt>AddUpdate.jsp</tt>]]
#:Create the page <tt>AddUpdate.jsp</tt> from: [[AddUpdate.jsp|AddUpdate.jsp]]
#::*Copy and paste the contents into the Page
#::*Copy and paste the contents into the Page
#::*Save the file as <tt>AddUpdate.jsp</tt>
#::*Save the file as <tt>AddUpdate.jsp</tt>
#:Create the page <tt>Directory.jsp</tt> from: [[Directory.jsp|<tt>Directory.jsp</tt>]]
#:Create the page <tt>Directory.jsp</tt> from: [[Directory.jsp|Directory.jsp]]
#::*Copy and paste the contents into the Page
#::*Copy and paste the contents into the Page
#::*Save the file as <tt>Directory.jsp</tt>
#::*Save the file as <tt>Directory.jsp</tt>
#:Create the page <tt>SearchPage.jsp</tt> from: [[SearchPage.jsp|<tt>SearchPage.jsp</tt>]]
#:Create the page <tt>SearchPage.jsp</tt> from: [[SearchPage.jsp|SearchPage.jsp]]
#::*Copy and paste the contents into the Page
#::*Copy and paste the contents into the Page
#::*Save the file as <tt>SearchPage.jsp</tt>
#::*Save the file as <tt>SearchPage.jsp</tt>
Line 210: Line 213:


After logging in, the user can perform these actions in the Directory object:
After logging in, the user can perform these actions in the Directory object:
*search for records based on specified criteria
:*search for records based on specified criteria
*add records
:*add records
*update records
:*update records


==MVC==
==MVC==
Line 247: Line 250:
:* You can set any Java object in <tt>controllerResponse</tt> by calling its <tt>setData</tt> method
:* You can set any Java object in <tt>controllerResponse</tt> by calling its <tt>setData</tt> method
|}
|}
Β 
<noinclude>


[[Category:Development]]
[[Category:Development]]
</noinclude>

Revision as of 00:49, 27 September 2011

In most cases, when you work with Pages and Classes, you want to separate the code that handles the user interface from the code that handles the data. In the platform, you do this by following the Model-View-Controller design pattern and creating two classes and a page.

Learn More:

How it Works

This section describes how a request flows from a page to the controller to the worker and how the result is displayed in the page.

Note: The code samples are presented here for illustration, and should not be used to build actual pages or classes. See Practical Example below for more on working code samples.

In this example, a page displays a search form for Directory object which is a built-in Object available to every account. The user can select the criteria to search the directory entries and then enter the string to search for. <syntaxhighlight lang="html4strict" enclose="div"> <form name="mainForm" action="/networking/controller/com/platform/demo/samples/DirectoryController" method="POST">

 <input type="hidden" name="action" value="search" />
Search
             <select name="searchType">
                 <option value="first_name">First Name</option>
                 <option value="name1">Last Name</option>
                 <option value="address">Address</option>
             </select>
for <input type="text" name="searchString" value="<%=searchString%>"
             />
<input type="submit" name="submit" value="Search" />

</form> </syntaxhighlight> The action attribute of the <form> tag specifies the URL of the class that you wrote that implements the Controller interface (DirectoryController in this example).

When the user clicks the Search button, the form is submitted to the DirectoryController's execute method, shown below.

execute Method

<syntaxhighlight lang="java" enclose="div">

import com.platform.api.*;

import java.util.HashMap;

public class DirectoryController implements Controller {

 public ControllerResponse execute(HashMap params) throws Exception
 {
   String action = (String)params.get("action");
   
   // ...
   ControllerResponse cr = new ControllerResponse();
   Directory directory = new Directory();
   Result result = null;
   if(action.equals("search"))
   {
     result = directory.search(params);
     cr.setTargetPage("directory.jsp");
   }
   // ...
   params.put("opResult", result);
   // ...
   cr.setData(params);
   return cr;
 }

} </syntaxhighlight>

This code:

  • Gets the value of the action key passed in params
  • Creates instances of the predefined classes ControllerResponse and Result
  • Creates an instance of the worker class Directory (shown below)
  • Calls the search method (shown below) in the worker class, if the action is "search", passing it params; also sets the next JSP name
  • Puts the result of the method call in params using the key opResult
  • Calls the setData method in ControllerResponse, specifying params as the argument
  • Returns the instance of ControllerResponse

search Method

Here is the search method in the Directory worker class.

<syntaxhighlight lang="java" enclose="div">

import com.platform.api.*;


public class Directory {

 // ...
 public Result search(HashMap params) throws Exception
 {
       Parameters addParams = Functions.getParametersInstance();
       addParams.add(params);
       String searchType = (String)params.get("searchType");
       String searchString = (String)params.get("searchString");
       Result result = null;
       if(searchType != null && !searchType.equals(""))
       {
         // Get the object ID from the GUI, by inspecting the object
         String directory_object_id = "361077714ltd1991022913";
         result = 
           Functions.searchRecords(directory_object_id,
               "record_id,first_name,address,city,state,zip,direct_phone,email",
               searchType + " contains '" + searchString + "'");
       }
       return result;
 }
 // ...

} </syntaxhighlight>

This code:

  • Creates an instance of Parameters and adds params to it
  • Gets the values of the searchType and searchString keys from params
  • Creates an instance of Result
  • Calls searchRecords, passing in the searchType and searchString and setting the Result object with the returned value
  • Returns the Result object

Displaying the Results

So, the search request originated in the page, went to the controller, and finally to the worker. Now we look at how the search results get displayed in the page.

This example:

  • Gets the opResult value from params and puts it into result
  • Iterates through result, creating a table row for each set of data

In the Page, here is the code that gets the search results:

<syntaxhighlight lang="java" enclose="div">

<%

 java.util.HashMap params = (java.util.HashMap)controllerResponse.getData();
 String searchType = params.get("searchType") == null ? "" : (String) params.get("searchType");
 String searchString = params.get("searchString") == null ? "" : (String) params.get("searchString");
 String message = params.get("message") == null ? "" : (String) params.get("message");

%> </syntaxhighlight>

The HTML and code below puts the search results into table rows.

<syntaxhighlight lang="java" enclose="div">

<% Result result = (Result)params.get("opResult"); if(result != null) { %>

<% ParametersIterator iter = result.getIterator(); while(iter.hasNext()) { Parameters row = iter.next();  %> <% } }  %>
Search Results:
<%=row.get("first_name")%> <%=row.get("address")%>

</syntaxhighlight>

Practical Example

JSP Pages are used to develop the UI to suit any custom requirements in an organization. The logic on how to perform the various operations on the server is present in the classes (Java code).

To try out the concepts of Pages and Classes, follow this tutorial to add real code to platform elements and build a .jsp page that can be used in a web browser.

This example describes how to use a Page to perform the following actions on records in the Directory object:

  • search for records based on specified criteria
  • add records
  • update records
Considerations
It is important to create the Classes files first, then create the Pages. Five files are provided, including two Classes, and three Pages. The files should be created in this order:
  1. Class Directory.java
  2. Class DirectoryController.java
  3. Page AddUpdate.jsp
  4. Page Directory.jsp
  5. Page SearchPage.jsp

To add the Pages and Classes:

  1. To create the (.java) Java pages, follow the instructions at Add a Class
    Create the class Directory.java from: Directory.java
    • Copy and paste the contents into the Class
    • Save the file as Directory.java
    Create the class DirectoryController.java from: DirectoryController.java
    • Copy and paste the contents into the Class
    • Save the file as DirectoryController.java
  2. To create the (.jsp) Java Server Pages, follow the instructions at Add a Page
    Create the page AddUpdate.jsp from: AddUpdate.jsp
    • Copy and paste the contents into the Page
    • Save the file as AddUpdate.jsp
    Create the page Directory.jsp from: Directory.jsp
    • Copy and paste the contents into the Page
    • Save the file as Directory.jsp
    Create the page SearchPage.jsp from: SearchPage.jsp
    • Copy and paste the contents into the Page
    • Save the file as SearchPage.jsp
  3. To invoke the Directory page from the URL, copy and paste the following into a web browser. A valid username and password is required to log in to the platform.
https://www.longjump.com/networking/pages/Directory.jsp

After logging in, the user can perform these actions in the Directory object:

  • search for records based on specified criteria
  • add records
  • update records

MVC

MVC: Model-View-Controller

  • A worker class that retrieves, updates, and deletes data (the Model)
  • A page that handles the user interface (the View)
  • A class that creates an instance of the worker class, call its methods, and sets the next page to display (the Controller)

The controller is a special class that implements the Controller interface. The Controller interface requires you to implement the execute method:

<syntaxhighlight lang="java" enclose="div">

ControllerResponse execute(HashMap params) </syntaxhighlight>

This method returns an instance of the ControllerResponse class. The platform uses the ControllerResponse class to send the appropriate response to the browser.

The argument params in the execute method contains the POST and GET data in the HTTP Request. If an HTML form is being submitted to the controller, the params HashMap contains all the <input> fields in the page. For each field:

  • The key of params is the name attribute of the <input> tag
  • The value of params is the value attribute of the <input> tag

Call these ControllerResponse methods to set the return value of the execute method:

  • setData: set the data to be made available to the next page
  • setTargetPage: set the name of the next page to display

In the controller, you create an instance of a worker class and call its methods to perform operations.

For details, see Controller Interface and ControllerResponse Class.

In a page, you have an implicit object called controllerResponse:

  • You call its getData method to get the data that was set by the controller
  • You can set any Java object in controllerResponse by calling its setData method