Difference between revisions of "Lab B.3: Adding Functionality"
From AgileApps Support Wiki
imported>Aeric |
imported>Aeric m (Text replace - '{domain}' to '{{domain}}') |
||
(26 intermediate revisions by the same user not shown) | |||
Line 13: | Line 13: | ||
Let's start by sending your name to the controller. | Let's start by sending your name to the controller. | ||
<ol> | <ol> | ||
<li>Click ''' | <li>Click '''[[File:GearIcon.png]] > Customization > Developer Resources > Pages''' | ||
<li>Click <tt>Hello.jsp</tt> | <li>Click <tt>Hello.jsp</tt> | ||
<li>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.) | <li>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.) | ||
Line 35: | Line 35: | ||
depending on the results. But for now, we'll just pass along the data.) | depending on the results. But for now, we'll just pass along the data.) | ||
<ol> | <ol> | ||
<li>Click ''' | <li>Click '''[[File:GearIcon.png]] > Customization > Developer Resources > Classes''' | ||
<li>Click <tt>HelloController</tt> | <li>Click <tt>HelloController</tt> | ||
<li>Add the code to echo the incoming data and pass it to the target page: | <li>Add the code to echo the incoming data and pass it to the target page: | ||
Line 41: | Line 41: | ||
public ControllerResponse execute(HashMap params) throws Exception | public ControllerResponse execute(HashMap params) throws Exception | ||
{ | { | ||
// Just to show we got | // Just to show we got the parameters, and could process them if we wanted to | ||
String name = (String) params.get("name"); | String name = (String) params.get("name"); | ||
Logger.info("HelloController: Hal will greet "+name, "HelloWorld"); | |||
// Pass the parameters to the target page | // Pass the parameters to the target page | ||
Line 72: | Line 72: | ||
// Just to show we got it, and could process it if we wanted to | // Just to show we got it, and could process it if we wanted to | ||
String name = (String) params.get("name"); | String name = (String) params.get("name"); | ||
Logger.info("HelloController: Hal will greet "+name, "HelloWorld"); | |||
// Pass the parameters to the target page | // Pass the parameters to the target page | ||
Line 85: | Line 85: | ||
===Customize the Destination Page=== | ===Customize the Destination Page=== | ||
<ol> | <ol> | ||
<li>Click ''' | <li>Click '''[[File:GearIcon.png]] > Customization > Developer Resources > Pages''' | ||
<li>Click <tt>HelloWorld.jsp</tt> | <li>Click <tt>HelloWorld.jsp</tt> | ||
<li>Put this code at the top of the page to | <li>Put this code at the top of the page to access the data sent by the controller, using the {{Implicit JSTL Object}}, <tt>param</tt>: | ||
:<syntaxhighlight lang="java" enclose="div"> | |||
<%@ taglib uri="/c" prefix="c" %> | |||
<h1 align="center">Hello ${param.name}!</h1> | |||
</syntaxhighlight> | |||
<li>Alternatively, use this code to process the data and to set a default value if none was provided. | |||
:<syntaxhighlight lang="java" enclose="div"> | :<syntaxhighlight lang="java" enclose="div"> | ||
<% | <% | ||
Line 94: | Line 100: | ||
if (controllerResponse != null) { | if (controllerResponse != null) { | ||
params = (java.util.HashMap)controllerResponse.getData(); | params = (java.util.HashMap)controllerResponse.getData(); | ||
if (params != null){ | if (params != null) { | ||
name = (String) params.get("name"); | name = (String) params.get("name"); | ||
name += ", my friend"; | |||
} | } | ||
} | } | ||
Line 101: | Line 108: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</li> | </li> | ||
<li> | <li>And then change the output line to use that value: | ||
:<syntaxhighlight lang="java" enclose="div"> | :<syntaxhighlight lang="java" enclose="div"> | ||
<h1 align="center">Hello <%=name %>!</h1> | <h1 align="center">Hello <%=name %>!</h1> | ||
Line 110: | Line 117: | ||
<tt>HelloWorld.jsp</tt> should now look like this: | <tt>HelloWorld.jsp</tt> should now look like this: | ||
:<syntaxhighlight lang="java" enclose="div"> | |||
<%@ taglib uri="/c" prefix="c" %> | |||
<h1 align="center">Hello ${param.name}!</h1> | |||
</syntaxhighlight> | |||
or this: | |||
:<syntaxhighlight lang="java" enclose="div"> | :<syntaxhighlight lang="java" enclose="div"> | ||
<% | <% | ||
Line 118: | Line 131: | ||
if (params != null){ | if (params != null){ | ||
name = (String) params.get("name"); | name = (String) params.get("name"); | ||
name += ", my friend"; | |||
} | } | ||
} | } | ||
Line 125: | Line 139: | ||
===Try It Out=== | ===Try It Out=== | ||
# Visit https:// | # Visit <nowiki>https://{{domain}}/networking/pages/Hello.jsp</nowiki>.<br>(For <tt>{{domain}}</tt>, substitute the URL you use to access the platform.) | ||
# In the platform, click ''' | # Type in a name and click the button to see the results. | ||
# In the platform, click '''[[File:GearIcon.png]] > [[File:GearIcon.png]] > Customization > Developer Resources > Debug Log''' to view the entry you made in the log.<br>If that window is already open, refresh it by clicking the "circling arrows" icon in the upper right: [[File:RefreshIcon.png]]. | |||
:You should see an entry like this: | :You should see an entry like this: | ||
::[[File:DebugMessage.jpg]] | ::[[File:DebugMessage.jpg]] | ||
==Learn More== | ==Learn More== | ||
:* | :* Platform [{{DOCHOST}}/javadocs/com/platform/api/ControllerResponse.html ControlerResponse] APIs | ||
:* | :*Platform [{{DOCHOST}}/javadocs/com/platform/api/Functions.html Functions] | ||
:* | :* [http://java.sun.com/developer/technicalArticles/Programming/jsp/ JSP syntax] | ||
:*{{Implicit JSTL Object}}s | |||
< | <br> | ||
;Next: | |||
: [[HowTo:Classes, APIs, and Naming Conventions]] | |||
<noinclude> | |||
[[Category:Lab]] | [[Category:Lab]] | ||
</noinclude> |
Latest revision as of 19:18, 25 April 2014
- 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
-
- Lab B.2: Simple Interaction, to create a controller and input page
Exercise
Pass a Value to the Controller
Let's start by sending your name to the controller.
- Click > Customization > Developer Resources > Pages
- Click Hello.jsp
- 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}" ... ~~~~~~~~~~~~~~~~
<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.)
- Click > Customization > Developer Resources > Classes
- Click HelloController
- 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 the parameters, and could process them if we wanted to String name = (String) params.get("name"); Logger.info("HelloController: Hal will greet "+name, "HelloWorld"); // 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"); Logger.info("HelloController: Hal will greet "+name, "HelloWorld"); // Pass the parameters to the target page ControllerResponse cr = new ControllerResponse(); cr.setData(params); cr.setTargetPage("HelloWorld.jsp"); return cr; } }
Customize the Destination Page
- Click > Customization > Developer Resources > Pages
- Click HelloWorld.jsp
- Put this code at the top of the page to access the data sent by the controller, using the Implicit JSTL Object, param:
<%@ taglib uri="/c" prefix="c" %> <h1 align="center">Hello ${param.name}!</h1>
- Alternatively, use this code to process the data and to set a default value if none was provided.
<% 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"); name += ", my friend"; } } %>
- And then change the output line to use that value:
<h1 align="center">Hello <%=name %>!</h1> ^^^^^^^^^^^^
HelloWorld.jsp should now look like this:
<%@ taglib uri="/c" prefix="c" %> <h1 align="center">Hello ${param.name}!</h1>
or 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"); name += ", my friend"; } } %> <h1 align="center">Hello <%=name %>!</h1>
Try It Out
- Visit https://{{domain}}/networking/pages/Hello.jsp.
(For {yourDomain}, substitute the URL you use to access the platform.) - Type in a name and click the button to see the results.
- In the platform, click > > Customization > Developer Resources > Debug Log to view the entry you made in the log.
If that window is already open, refresh it by clicking the "circling arrows" icon in the upper right: .
Learn More
- Platform ControlerResponse APIs
- Platform Functions
- JSP syntax
- Implicit JSTL Objects