Difference between revisions of "Lab B.3: Adding Functionality"
imported>Aeric |
imported>Aeric |
||
Line 150: | Line 150: | ||
:* [http://java.sun.com/developer/technicalArticles/Programming/jsp/ JSP syntax] | :* [http://java.sun.com/developer/technicalArticles/Programming/jsp/ JSP syntax] | ||
:*{{Implicit JSTL Object}}s | :*{{Implicit JSTL Object}}s | ||
<br> | |||
;Next: | ;Next: | ||
: [[HowTo:Create a JSP Page and Java Controller]] | : [[HowTo:Create a JSP Page and Java Controller]] |
Revision as of 23:56, 3 August 2012
- 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 Designer > 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.)
- <syntaxhighlight lang="java" enclose="div">
Press the button to see something cool.
<form name="mainForm" action="/networking/controller/com/platform/demo/hello/HelloController?name={yourName}"
... ~~~~~~~~~~~~~~~~
</syntaxhighlight> For example:
- <syntaxhighlight lang="java" enclose="div">
Press the button to see something cool.
<form name="mainForm" action="/networking/controller/com/platform/demo/hello/HelloController?name=Fred"
...
</syntaxhighlight>
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 Designer > Classes
- Click HelloController
- Add the code to echo the incoming data and pass it to the target page:
- <syntaxhighlight lang="java" enclose="div">
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:
- <syntaxhighlight lang="java" enclose="div">
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; }
} </syntaxhighlight>
Customize the Destination Page
- Click Designer > 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:
- <syntaxhighlight lang="java" enclose="div">
Hello ${param.name}!
</syntaxhighlight>
- Alternatively, use this code to process the data and to set a default value if none was provided.
- <syntaxhighlight lang="java" enclose="div">
- And then change the output line to use that value:
- <syntaxhighlight lang="java" enclose="div">
Hello <%=name %>!
^^^^^^^^^^^^
</syntaxhighlight>
HelloWorld.jsp should now look like this:
- <syntaxhighlight lang="java" enclose="div">
<%@ taglib uri="/c" prefix="c" %>
Hello ${param.name}!
</syntaxhighlight>
or this:
- <syntaxhighlight lang="java" enclose="div">
<%
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"; } }
%>
Hello <%=name %>!
</syntaxhighlight>
Try It Out
- Visit https://{domain}/networking/pages/Hello.jsp.
(For {domain}, 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 Settings > Administration > Global 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