You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by mr...@apache.org on 2005/03/22 05:47:04 UTC

svn commit: r158535 - in struts/core/trunk/doc/flow: extensions.html index.xml project.xml remote-example.xml

Author: mrdon
Date: Mon Mar 21 20:47:02 2005
New Revision: 158535

URL: http://svn.apache.org/viewcvs?view=rev&rev=158535
Log:
Adding more documentation to the Struts Flow site and an additional
example.

Added:
    struts/core/trunk/doc/flow/extensions.html
    struts/core/trunk/doc/flow/remote-example.xml   (with props)
Modified:
    struts/core/trunk/doc/flow/index.xml
    struts/core/trunk/doc/flow/project.xml

Added: struts/core/trunk/doc/flow/extensions.html
URL: http://svn.apache.org/viewcvs/struts/core/trunk/doc/flow/extensions.html?view=auto&rev=158535
==============================================================================
--- struts/core/trunk/doc/flow/extensions.html (added)
+++ struts/core/trunk/doc/flow/extensions.html Mon Mar 21 20:47:02 2005
@@ -0,0 +1,246 @@
+<html>
+<head>
+    <title>Java API Extensions</title>
+</head>
+<body>
+<h2>Java API Extensions</h2>
+<p>
+The following are methods that are added to Java API classes to
+make developing with Struts Flow easier.
+</p>
+
+  <h3>java.util.Collection</h3>
+  <table width="100%" border="1">
+      <tr>
+        <td><small>int</small></td>
+        <td><a href="#CollectionExtensions_length">length</a> - Provides the current size of the collection.</td>
+      </tr>  
+  </table>
+  <br />
+  <table width="100%" border="1">
+      <tr>
+        <td><small>void</small></td>
+        <td><a href="#CollectionExtensions_each">each(Function func)</a> - Iterates through the collection and for each element, calls the passed function with the element as the parameter.</td>
+      </tr>  
+      <tr>
+        <td><small>Object</small></td>
+        <td><a href="#CollectionExtensions_find">find(Function func)</a> - Finds the first item selected by the passed function.</td>
+      </tr>  
+      <tr>
+        <td><small>java.util.List</small></td>
+        <td><a href="#CollectionExtensions_findAll">findAll(Function func)</a> - Finds all items selected by the passed function.</td>
+      </tr>  
+  </table>
+
+  <h3>java.util.List</h3>
+  <table width="100%" border="1">
+  </table>
+  <br />
+  <table width="100%" border="1">
+      <tr>
+        <td><small>java.util.List</small></td>
+        <td><a href="#ListExtensions_asImmutable">asImmutable()</a> - Returns an immutable version of this list.</td>
+      </tr>  
+      <tr>
+        <td><small>java.util.List</small></td>
+        <td><a href="#ListExtensions_asSynchronized">asSynchronized()</a> - Returns a synchronized version of this list.</td>
+      </tr>  
+      <tr>
+        <td><small>Object</small></td>
+        <td><a href="#ListExtensions_pop">pop()</a> - Pops the last item off the list.</td>
+      </tr>  
+      <tr>
+        <td><small>java.util.List</small></td>
+        <td><a href="#ListExtensions_sort">sort()</a> - Sorts the list according to the natural order.</td>
+      </tr>  
+      <tr>
+        <td><small>java.util.List</small></td>
+        <td><a href="#ListExtensions_sortEach">sortEach(Function func)</a> - Sorts the list using the passed function to determine order.</td>
+      </tr>  
+  </table>
+
+  <h3>java.io.File</h3>
+  <table width="100%" border="1">
+  </table>
+  <br />
+  <table width="100%" border="1">
+      <tr>
+        <td><small>java.io.File</small></td>
+        <td><a href="#FileExtensions_append">append(String text)</a> - Appends text to the file.</td>
+      </tr>  
+      <tr>
+        <td><small>String</small></td>
+        <td><a href="#FileExtensions_getText">getText()</a> - Gets the contents of the file as a String.</td>
+      </tr>  
+      <tr>
+        <td><small>void</small></td>
+        <td><a href="#FileExtensions_eachLine">eachLine(Function func)</a> - Passes each line to the provided function.</td>
+      </tr>  
+  </table>
+
+<hr />
+
+  <h3>java.util.Collection</h3>
+        <hr />
+        <a name="CollectionExtensions_length" />
+        <h4>length</h4>
+        <code> length</code>
+        <blockquote>
+         <p>
+           Provides the current size of the collection. Alternative to the size() method to be more consistent with Javascript arrays.
+         </p>
+         <p>
+         <strong>Example(s):</strong><br />
+         <code>list.length</code>
+         </p>
+         </blockquote>
+        <hr />
+        <a name="CollectionExtensions_each" />
+        <h4>each</h4>
+          <code>void each(Function func)</code>
+        <blockquote>
+         <p>
+           Iterates through the collection and for each element, calls the passed function with the element as the parameter.
+         </p>
+         <p>
+         <strong>Example(s):</strong><br />
+         <code>list.each(function(item) { print(item) })</code>
+         </p>
+         </blockquote>
+        <hr />
+        <a name="CollectionExtensions_find" />
+        <h4>find</h4>
+          <code>Object find(Function func)</code>
+        <blockquote>
+         <p>
+           Finds the first item selected by the passed function. The function will receive the item and should return true or false for the result.
+         </p>
+         <p>
+         <strong>Example(s):</strong><br />
+         <code>item = list.find(function(item) { return item.matches(/foo[0-9]/) })</code>
+         </p>
+         </blockquote>
+        <hr />
+        <a name="CollectionExtensions_findAll" />
+        <h4>findAll</h4>
+          <code>java.util.List findAll(Function func)</code>
+        <blockquote>
+         <p>
+           Finds all items selected by the passed function. The function will receive the item and should return true or false for the result. A list of all matches will be returned.
+         </p>
+         <p>
+         <strong>Example(s):</strong><br />
+         <code>matches = list.findAll(function(item) { return item.matches(/foo[0-9]/) })</code>
+         </p>
+         </blockquote>
+
+  <h3>java.util.List</h3>
+        <hr />
+        <a name="ListExtensions_asImmutable" />
+        <h4>asImmutable</h4>
+          <code>java.util.List asImmutable()</code>
+        <blockquote>
+         <p>
+           Returns an immutable version of this list.
+         </p>
+         <p>
+         <strong>Example(s):</strong><br />
+         <code>frozenList = list.asImmutable()</code>
+         </p>
+         </blockquote>
+        <hr />
+        <a name="ListExtensions_asSynchronized" />
+        <h4>asSynchronized</h4>
+          <code>java.util.List asSynchronized()</code>
+        <blockquote>
+         <p>
+           Returns a synchronized version of this list.
+         </p>
+         <p>
+         <strong>Example(s):</strong><br />
+         <code>multiThreadList = list.asSynchronized()</code>
+         </p>
+         </blockquote>
+        <hr />
+        <a name="ListExtensions_pop" />
+        <h4>pop</h4>
+          <code>Object pop()</code>
+        <blockquote>
+         <p>
+           Pops the last item off the list. The last item will be returned and removed from the list.
+         </p>
+         <p>
+         <strong>Example(s):</strong><br />
+         <code>lastItem = list.pop()</code>
+         </p>
+         </blockquote>
+        <hr />
+        <a name="ListExtensions_sort" />
+        <h4>sort</h4>
+          <code>java.util.List sort()</code>
+        <blockquote>
+         <p>
+           Sorts the list according to the natural order.
+         </p>
+         <p>
+         <strong>Example(s):</strong><br />
+         <code>sortedList = list.sort()</code>
+         </p>
+         </blockquote>
+        <hr />
+        <a name="ListExtensions_sortEach" />
+        <h4>sortEach</h4>
+          <code>java.util.List sortEach(Function func)</code>
+        <blockquote>
+         <p>
+           Sorts the list using the passed function to determine order. The function will receive two parameters, and should return &gt; 0 if the first is greater, &lt; 0 if the first is less, and 0 if equal.
+         </p>
+         <p>
+         <strong>Example(s):</strong><br />
+         <code>sortedList = list.sort(function(val1, val2) { return val1.compareTo(val2) })</code>
+         </p>
+         </blockquote>
+
+  <h3>java.io.File</h3>
+        <hr />
+        <a name="FileExtensions_append" />
+        <h4>append</h4>
+          <code>java.io.File append(String text)</code>
+        <blockquote>
+         <p>
+           Appends text to the file.
+         </p>
+         <p>
+         <strong>Example(s):</strong><br />
+         <code>file.append("added text")</code>
+         </p>
+         </blockquote>
+        <hr />
+        <a name="FileExtensions_getText" />
+        <h4>getText</h4>
+          <code>String getText()</code>
+        <blockquote>
+         <p>
+           Gets the contents of the file as a String.
+         </p>
+         <p>
+         <strong>Example(s):</strong><br />
+         <code>text = file.getText()</code>
+         </p>
+         </blockquote>
+        <hr />
+        <a name="FileExtensions_eachLine" />
+        <h4>eachLine</h4>
+          <code>void eachLine(Function func)</code>
+        <blockquote>
+         <p>
+           Passes each line to the provided function. The file is opened, and interpreted as a text file using the default encoding. Each line is read and passed to the provided function.
+         </p>
+         <p>
+         <strong>Example(s):</strong><br />
+         <code>file.eachLine(function(line) { print(line) })</code>
+         </p>
+         </blockquote>
+
+</body>
+</html>

Modified: struts/core/trunk/doc/flow/index.xml
URL: http://svn.apache.org/viewcvs/struts/core/trunk/doc/flow/index.xml?view=diff&r1=158534&r2=158535
==============================================================================
--- struts/core/trunk/doc/flow/index.xml (original)
+++ struts/core/trunk/doc/flow/index.xml Mon Mar 21 20:47:02 2005
@@ -17,7 +17,25 @@
 
         </p>
         <p>
-        While the initial target of the extracted Control Flow is Struts, the Flow code is reusable from other 
+        Since continuations are best implemented in Javascript, Struts Flow also focuses on making Javascript
+        easier to use and better integrated into the Java environment borrowing several ideas from <a 
+        href="http://groovy.codehaus.org">Groovy</a>.  First, information in Collections classes
+        can be accessed using Javascript array (<code>foo[1]</code>) and object (<code>foo["bar"]</code>) notations.
+        Also, Struts Flow uses <a href="http://www.json.org">JSON</a> to allow client-side Javascript running
+        in a browser to call server-side Javascript flow functions.  Finally, core Java API classes like <code>java.io.File</code> have additonal methods and properties
+        added to them.  For example, to process each line of a text file, you can use the following closure:
+        </p>
+<pre>
+  file = new java.io.File("foo.txt");
+  file.eachLine(
+    function(line) {
+      print(line);
+    }
+  );
+</pre>
+        <p>
+        While the initial target of the extracted Control Flow is Struts, the Flow code and Javascript extensions
+        are reusable from other 
         non-Struts environments.  This means Control Flow could be used to drive non-Struts JSP applications, portlets, or even complex web services.
         </p>
     </section>  
@@ -27,12 +45,24 @@
               <li>Full access to Struts features</li>
               <li>Can exist side-by-side regular Struts actions</li>
               <li>Ability to run in non-Struts environments (uses Jakarta's Commons-Chain)</li>
+              <li><a href="extensions.html">Enhanced Java API methods</a> and Collections integration</li>
+              <li>Remote RPC support for calling flow methods from the client</li>
               <li>Includes Wizard library to help easily create complex wizards</li> 
-              <li>Includes Wizard example</li> 
-              <li>Includes Cocoon's number guessing game example</li> 
+              <li>Includes number guessing, remote rpc, and wizard examples</li> 
             </ul>
       </section>
       <section name="What's New" href="new">
+        <section name="0.3 - Unreleased">
+            <ul>
+              <li>Added better Collections support within Javascript</li>
+              <li>Upgraded Rhino library to 1.6 which features native continuations support</li>
+              <li>Added framework for adding methods and properties to core Java API classes</li>
+              <li>Added JSON support</li>
+              <li>Added client Javascript library and server-side support for calling flow functions from the brower</li>
+              <li>Moved project to Apache Struts</li>
+              <li>Added new remote number guessing game example</li>
+            </ul>
+        </section>
         <section name="0.2 - September 10, 2004">
             <ul>
               <li>Added wizard library to make wizard creation easy</li>
@@ -71,6 +101,7 @@
         </p>
         <ul>
             <li><a href="guess-example.html">Number Guess Game Example</a></li>
+            <li><a href="remote-example.html">Number Guess Game Example - Remote Edition</a></li>
             <li><a href="wizard-example.html">Wizard Example</a></li>
         </ul>
       </section>

Modified: struts/core/trunk/doc/flow/project.xml
URL: http://svn.apache.org/viewcvs/struts/core/trunk/doc/flow/project.xml?view=diff&r1=158534&r2=158535
==============================================================================
--- struts/core/trunk/doc/flow/project.xml (original)
+++ struts/core/trunk/doc/flow/project.xml Mon Mar 21 20:47:02 2005
@@ -9,6 +9,7 @@
         <item name="Welcome" href="index.html"/>
         <item name="What's New" href="index.html#new"/>
         <item name="Requirements" href="index.html#requirements"/>
+        <item name="Java Enhancements" href="extensions.html"/>
         <item name="Examples" href="index.html#examples"/>
         <item name="Download (Sourceforge)" href="http://sourceforge.net/project/showfiles.php?group_id=49385&amp;package_id=120079"/>
     </menu>

Added: struts/core/trunk/doc/flow/remote-example.xml
URL: http://svn.apache.org/viewcvs/struts/core/trunk/doc/flow/remote-example.xml?view=auto&rev=158535
==============================================================================
--- struts/core/trunk/doc/flow/remote-example.xml (added)
+++ struts/core/trunk/doc/flow/remote-example.xml Mon Mar 21 20:47:02 2005
@@ -0,0 +1,125 @@
+<?xml version="1.0"?>
+<document url="remote-example.html">
+
+  <properties>
+    <title>Struts Flow - Remote Example</title>
+  </properties>
+
+  <body>
+      <section name="Number Guessing Game Example - Remote Edition" href="overview">
+        <p>This example shows the number guessing game demonstrated in the <a href="guess-example.html">previous example</a>,
+        but now we add a browser remote function call to show how easy it is to call server-side Javascript functions
+        from client-side Javascript.
+        Struts Flow provides a client Javascript library and server-side framework support for calling flow functions
+        remotely from the client's web browser.
+        Function calls on the server are executed in the 
+        flow so the global variable state is available to be read and modified.  In this example, we add a "cheat"
+        button that will let the user see the secret number, but penalize their number of guesses by 5.  The
+        two new parts to the example are in the flow code and the guess.jsp file.
+        </p>
+      </section>
+      <section name="Flow Code" href="flow">
+        <p>Here is what the flow code looks like:
+        </p>
+<pre>
+var random;
+var guesses;
+
+function main() {
+
+  random =  Math.round( Math.random() * 9 ) + 1;
+  var hint = "No hint for you!"
+  guesses = 0;
+
+  while (true) {
+
+    // send guess page to user and wait for response.  Specify only cheat() 
+    // can be called remotely.
+    forwardAndWait("failure", 
+       { "hint"    : hint,
+         "guesses" : guesses},
+       ["cheat"]);
+
+    print("processing a user guess "+getRequestParams().guess);
+    // process user's guess
+    var guess = parseInt( getRequestParams().guess );
+    guesses++;
+    if (guess) {
+      if (guess > random) {
+        hint = "Nope, lower!"
+      } 
+      else if (guess &lt; random) {
+        hint = "Nope, higher!"
+      } 
+      else {
+        // correct guess
+        break;
+      }
+    }
+  }
+
+  // send success page to user
+  forwardAndWait("success", 
+     {"random"  : random, 
+      "guess"   : guess, 
+      "guesses" : guesses} );
+}
+
+function cheat() {
+    guesses += 5;
+    return {"secret":random, "guesses":guesses};
+}
+</pre>
+        <p>The big change here is several variables have been moved to outside the function to allow access by
+        the new cheat() function.  The cheat() function adds 5 to the number of guesses then returns the secret
+        number.  Struts Flow converts the returned object into <a href="http://www.json.org">JSON</a> and returns
+        it to the client.</p>
+    </section>
+ <section name="JSP Presentation" href="jsp">
+        <p>This example adds the "cheat" button to the number guessing form, <code>guess.jsp</code>.  When
+        pressed, the Javascript uses the clientFlow.js library to call the cheat() function on the server.</p>
+<pre>
+&lt;?xml version="1.0"?>
+&lt;html>
+&lt;head>
+  &lt;title>Struts Flow number guessing game&lt;/title>
+    &lt;script type="text/javascript">
+  &lt;!--
+function init() {
+    this.contid = "&lt;%=request.getAttribute("contid")%>";
+    this.client = new ClientFlow("guess.do");
+}   
+function cheat() {
+    result = client.call("cheat", contid);
+    alert("The secret number is "+result.secret+". After applying a penalty, you have guessed "+result.guesses+" times");
+    contid = result.contid;
+}
+    -->
+  &lt;/script>
+  &lt;script type="text/javascript" src="clientFlow.js" />
+&lt;/head>
+&lt;body onload="init()">
+
+  &lt;h1>Guess the Number Between 1 and 10&lt;/h1>
+  
+  &lt;h2>&lt;%= request.getAttribute("hint") %>&lt;/h2>
+  
+  &lt;h3>You've guessed &lt;%= request.getAttribute("guesses") %> times.&lt;/h3>
+  
+  &lt;form method="post" action="guess.do">
+    &lt;input type="hidden" name="contid" value='&lt;%= request.getAttribute("contid") %>' />
+    &lt;input type="text" name="guess"/>
+    &lt;input type="submit"/>
+    &lt;input type="button" onclick="cheat()" value="Cheat" />
+  &lt;/form>
+  
+&lt;/body>
+&lt;/html>
+</pre>
+<p>The key to the function call executing in the server flow is passing the continuation id to the client flow
+instance.
+</p>
+        
+      </section>
+  </body>
+</document>

Propchange: struts/core/trunk/doc/flow/remote-example.xml
------------------------------------------------------------------------------
    svn:executable = *



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org