Difference between revisions of "HowTo:Create a JSP Page and Java Controller"

From AgileApps Support Wiki
imported>Aeric
imported>Aeric
m (Text replace - '{domain}' to '{{domain}}')
Β 
(45 intermediate revisions by the same user not shown)
Line 1: Line 1:
<noinclude>
<noinclude>
{{Orientation|Developers|Intermediate|15}}
{{Orientation|Developers|Intermediate|30}}
</noinclude>This example describes how to use a [[Page]] to perform the following actions on records in the ''Directory'' object:
</noinclude>==Overview==
:*search for records based on specified criteria
In this guide, you create a JSP [[Page]] and a Controller [[Class]] that interact to:
:*add records
:*Add records to a Mailing List object Β 
:*update records
:*Do a SQL search for (one or more) records Β 
:*Update records
===Sample Scenario===
A sample interaction scenario might look like this:
:[[File:jsp_controller_scenario.png]]
::* The arrows going to the controller show the value of the <tt>action</tt> variable that is passed from the JSP Page.
::* The arrows going to the JSP Page show the value of the <tt>control</tt> variable that is passed from the controller.
::: (The names of the variables are determined by convention. They could be anything.)


;Considerations:
===Communication Mechanisms===
: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:
This diagram shows how the interactions between the JSP Page and the controller Class take place:
#Class [[Directory.java|Directory.java]]
:[[File:jsp_controller_interactions.png]]
#Class [[DirectoryController.java|DirectoryController.java]]
::* An HTML <tt>Form</tt> designates the location to go to when a button is pressed.
#Page [[AddUpdate.jsp|AddUpdate.jsp]]
::* In this case, the Form targets a Class.<br>The platform instantiates an object when the class page is visited.
#Page [[Directory.jsp|Directory.jsp]]
::* All contents of the form are passed to the controller as name/value pairs, in the <tt>params</tt> argument, including:
#Page [[SearchPage.jsp|SearchPage.jsp]]
::: - Fields with data the user supplies: <tt>&lt;input type="input" ...</tt>
::: - Hidden data fields: <tt>&lt;input type="hidden" value="<%=SomeVariable%>"...</tt>
::: - The button that was pressed: <tt>&lt;input type="submit" name="..." value="..." ...</tt>
::::Β  (The value is displayed as the button label, and passed as the value associated with the name.)
:::* The controller's <tt>execute</tt> method returns a <tt>ControllerResponse</tt> object
:::* The <tt>ControllerResponse</tt> object designates the page to go to next
:::* You use its <tt>setData</tt> method to load a HashMap with values to send to the page


To add the Pages and Classes:
==Prerequisites==
#To create the (.java) Java pages, follow the instructions at [[Classes#Adding_a_Class|Add a Class]]
:* A "Mailing List" object must be created, with two fields: <tt>contact_name</tt> and <tt>contact_email</tt>
#:Create the class Directory.java from: [[Directory.java|Directory.java]]
#::*Copy and paste the contents into the Class
#::*Save the file as Directory.java
#:Create the class DirectoryController.java from: [[DirectoryController.java|DirectoryController.java]]
#::*Copy and paste the contents into the Class
#::*Save the file as DirectoryController.java
#To create the (.jsp) Java Server Pages, follow the instructions at [[Pages#Adding_a_Page|Add a Page]]
#:Create the page AddUpdate.jsp from: [[AddUpdate.jsp|AddUpdate.jsp]]
#::*Copy and paste the contents into the Page
#::*Save the file as AddUpdate.jsp
#:Create the page Directory.jsp from: [[Directory.jsp|Directory.jsp]]
#::*Copy and paste the contents into the Page
#::*Save the file as Directory.jsp
#:Create the page SearchPage.jsp from: [[SearchPage.jsp|SearchPage.jsp]]
#::*Copy and paste the contents into the Page
#::*Save the file as SearchPage.jsp
#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.
::<tt>{{platformURL}}/pages/Directory.jsp</nowiki></tt>


After logging in, the user can perform these actions in the Directory object:
==Developing the Application==
:*search for records based on specified criteria
===Create the Java class===
:*add records
# Follow the instructions at [[Classes#Add a Class|Add a Class]] to create <tt>AddUpdateController_YOURNAME</tt>
:*update records
# Copy and paste the contents of [{{DOCHOST}}/training/AddUpdateController.java AddUpdateController.java] into the Class
# Modify the class, supplying your package namespace and name:
#: [[File:PersonalizeController.png]]
# Save the class
Β 
===Create the JSP Page===
# Follow the instructions at [[Pages#Add_a_Page|Add a Page]] to create <tt>AddUpdateYOURNAME.jsp</tt>
# Copy and paste the contents of [{{DOCHOST}}/training/AddUpdate.jsp AddUpdate.jsp]
# Customize the JSP Page with your package namespace and name:
#: [[File:personalizeJSP.png]] <!--OLD VERSION W/GOOD HIGHLIGHTING: personalize_jsp.png -->
# Save the JSP page
Β 
===Try it Out===
To test the project you created, visit the URL for your page, substituting your platform address for "<tt>{{domain}}</tt>":
:<tt>https://{{domain}}/pages/AddUpdateYOURNAME.jsp</tt><br>(A valid username and password is required to log in to the platform.)

Latest revision as of 19:17, 25 April 2014

For:   Developers
Level: Intermediate
Time: 30 minutes

See more:
    ◾ HowTo Guides

Overview

In this guide, you create a JSP Page and a Controller Class that interact to:

  • Add records to a Mailing List object
  • Do a SQL search for (one or more) records
  • Update records

Sample Scenario

A sample interaction scenario might look like this:

Jsp controller scenario.png
  • The arrows going to the controller show the value of the action variable that is passed from the JSP Page.
  • The arrows going to the JSP Page show the value of the control variable that is passed from the controller.
(The names of the variables are determined by convention. They could be anything.)

Communication Mechanisms

This diagram shows how the interactions between the JSP Page and the controller Class take place:

Jsp controller interactions.png
  • An HTML Form designates the location to go to when a button is pressed.
  • In this case, the Form targets a Class.
    The platform instantiates an object when the class page is visited.
  • All contents of the form are passed to the controller as name/value pairs, in the params argument, including:
- Fields with data the user supplies: <input type="input" ...
- Hidden data fields: <input type="hidden" value="<%=SomeVariable%>"...
- The button that was pressed: <input type="submit" name="..." value="..." ...
(The value is displayed as the button label, and passed as the value associated with the name.)
  • The controller's execute method returns a ControllerResponse object
  • The ControllerResponse object designates the page to go to next
  • You use its setData method to load a HashMap with values to send to the page

Prerequisites

  • A "Mailing List" object must be created, with two fields: contact_name and contact_email

Developing the Application

Create the Java class

  1. Follow the instructions at Add a Class to create AddUpdateController_YOURNAME
  2. Copy and paste the contents of AddUpdateController.java into the Class
  3. Modify the class, supplying your package namespace and name:
    PersonalizeController.png
  4. Save the class

Create the JSP Page

  1. Follow the instructions at Add a Page to create AddUpdateYOURNAME.jsp
  2. Copy and paste the contents of AddUpdate.jsp
  3. Customize the JSP Page with your package namespace and name:
    PersonalizeJSP.png
  4. Save the JSP page

Try it Out

To test the project you created, visit the URL for your page, substituting your platform address for "{yourDomain}":

https://{yourDomain}/pages/AddUpdateYOURNAME.jsp
(A valid username and password is required to log in to the platform.)