2022年11月30日水曜日

Cozy Web/HelloScript

前回はCozy Scriptの組込み関数を使用して、システムが提供するオペレーションをフォーム画面から呼出して使用する方法について説明しました。

今回はフォーム画面から入力したパラメタを引数にしてCozy Scriptを実行する方法について説明します。

HelloScript

フォームから使用できるアプリケーション・ロジックをCozy Scriptのスクリプトとして記述することができます。

Cozy scriptを用いたWebアプリケーションHelloScriptを作成します。

準備

cozyを起動するディレクトリのwebappsディレクトリに、アプリケーションのホームディレクトリとなるHelloScriptを作成します。

Formページ

フォームを使った入力画面として以下のページをindex.jadeとしてホームディレクトリに配置します。

  1. -@val form: ViewForm  
  2. html  
  3.   head  
  4.     title HelloScript  
  5.   body  
  6.     form(method="POST" action={form.action})  
  7.       input(hidden="true" name="$scenario" value={form.scenario})  
  8.       input(name="arg1" value={form.arg1})  
  9.       input(name="arg2" value={form.arg2})  
  10.       input(name="arg3" value={form.arg3})  
  11.       button(type="submit" id="submitbutton" name="$submit" value="ok") Submit  
  12.       button(type="submit" id="cancelbutton" name="$submit" value="cancel") Cancel  

ViewFormオブジェクト

以下の宣言でフォームの設定に必要なViewFormを参照可能にします。

  1. -@val form: ViewForm  

Form/action

actionにViewFormオブジェクトのaction属性を設定します。

  1. form(method="POST" action={form.action})  

今回の場合は空文字が設定されます。入力と同じページにフォームのPOSTが行われるということですね。

POST処理に必要な情報は後述する「$scenario」に設定されています。

Input/Hidden

Hidden属性のInputで入力する「$scenario」プロパティにViewFormオブジェクトのscenario属性を設定します。

  1. input(hidden="true" name="$scenario" value={form.scenario})  

今回の場合は「{"name":"invoke-operation","state":"input","data":{}}」が設定されています。URLエンコーディングを解除した値は「{"name":"invoke-operation","state":"input","data":{}}」です。

Cozy Webがフォーム処理を行うために必要な情報が設定されています。

Inputデータ

Hidden属性のないInputでデータ入力を行います。

今回は以下の3行が対象です。

  1. input(name="arg1" value={form.arg1})  
  2. input(name="arg2" value={form.arg2})  
  3. input(name="arg3" value={form.arg3})  

nameにarg1,arg2,arg3を指定しているので、arg1プロパティとarg2プロパティ, arg3プロパティの3つのプロパティの入力ということになります。値のデフォルト値としてViewFormオブジェクトのarg1属性とarg2属性の値を設定しています。今回のケースでは空文字が設定されます。

Input/Ok

OK用のサブミットボタンとして以下のButtonを設定しました。

  1. button(type="submit" id="submitbutton" name="$submit" value="ok") Submit  

name属性に「$submit」、value属性に「ok」を指定しています。

Input/Cancel

キャンセル用のサブミットボタンとして以下のButtonを設定しました。

  1. button(type="submit" id="cancelbutton" name="$submit" value="cancel") Cancel  

name属性に「$submit」、value属性に「cancel」を指定しています。

コントローラ

WEB-INF/controllersにindex.jade用のコントローラである以下のindex.jsonを配置します。

  1. {  
  2.   "action""script-scenario",  
  3.   "script0""(+ 1 2 3)",  
  4.   "script""(+ arg1 (* arg2 arg3))",  
  5.   "method""POST",  
  6.   "successView""index_complete",  
  7.   "errorView""index_error",  
  8.   "parameters": [{  
  9.     "name""arg1",  
  10.     "datatype""int"  
  11.   },{  
  12.     "name""arg2",  
  13.     "datatype""int"  
  14.   },{  
  15.     "name""arg3",  
  16.     "datatype""int"  
  17.   }]  
  18. }  

action

コントローラのアクションとしてscript-scenarioを指定しています。

  1. "action""script-scenario",  

script-scenarioは、フォームで入力したパラメタ入力を使ってスクリプトを呼び出し、その結果をビューに渡すモデルとして生成する処理を行うシナリオです。

フォーム入力にまつわるWebブラウザとサーバ間のインタラクションをシナリオに従って実現します。

script

実行するスクリプトをCozy Scriptで記述します。

  1. "script""(+ arg1 (* arg2 arg3))",  

method

メソッドはPOSTを指定しています。

  1. "method""POST",  

FormのメソッドがPOSTのものを受け付けます。

successView

successViewにはindex_completeを指定しています。

  1. "successView""index_complete",  

コントローラの処理が成功するとこのページに遷移します。

errorView

errorViewにはindex_errorを指定しています。

  1. "errorView""index_error",  

コントローラの処理が失敗するとこのページに遷移します。

parameters

フォームから入力されるパラメタとして、パラメタ名とデータ型を指定しています。

  1. "parameters": [{  
  2.   "name""arg1",  
  3.   "datatype""int"  
  4. },{  
  5.   "name""arg2",  
  6.   "datatype""int"  
  7. },{  
  8.   "name""arg3",  
  9.   "datatype""int"  
  10. }]  

パラメタは、パラメタ名arg1でデータ型int, パラメタ名arg2でデータ型int, パラメタ名arg3でデータ型intの3つです。

成功ページ

成功ページとして以下のindex_complete.htmlを用意します。コントローラのsuccessViewで指定したページです。

  1. <html>  
  2.     <head>  
  3.     <title>Script Success</title>  
  4.     </head>  
  5.     <body>  
  6.     <h1>Script Success</h1>  
  7.     <c:model/>  
  8.     </body>  
  9. </html>  

このページ内の以下のタグはコントローラの実行結果のモデルの内容を表形式で表示するものです。

  1. <c:model/>  

今回の場合は、スクリプトの実行結果が出力されます。

エラーページ

成功ページとして以下のindex_error.htmlを用意します。コントローラのerrorViewで指定したページです。

  1. <html>  
  2.     <head>  
  3.     <title>Script Error</title>  
  4.     </head>  
  5.     <body>  
  6.     <h1>Script Error</h1>  
  7.     <c:error/>  
  8.     </body>  
  9. </html>  

このページ内の以下のタグはコントローラの実行時にエラーが発生した場合、そのエラーを表形式で表示するものです。

  1. <c:error/>  

今回の場合は、スクリプトの実行結果がエラーとなる場合に出力されます。

実行

Formページ

curlコマンドによってローカルホストの8080ポート上の/web/HelloScript を取得します。

  1. $ curl http://localhost:8080/web/HelloScript/  

以下のHTML文書が返されます。

  1. <!DOCTYPE html>  
  2. <html>  
  3.   <head>  
  4.     <title>HelloScript</title>  
  5.   </head>  
  6.   <body>  
  7.     <form action="" method="POST">  
  8.       <input value="{&quot;name&quot;:&quot;execute-script&quot;,&quot;state&quot;:&quot;input&quot;,&quot;data&quot;:{}}" name="$scenario" hidden="true" />  
  9.       <input value="" name="arg1" />  
  10.       <input value="" name="arg2" />  
  11.       <input value="" name="arg3" />  
  12.       <button value="ok" name="$submit" id="submitbutton" type="submit">Submit</button>  
  13.       <button value="cancel" name="$submit" id="cancelbutton" type="submit">Cancel</button>  
  14.     </form>  
  15.   </body>  
  16. </html>  

OK

OKボタンの押下は以下のcurlに相当します。

  1. $ curl http://localhost:8080/web/HelloScript/ -X POST \  
  2. --data-urlencode '$submit=ok' \  
  3. --data-urlencode 'arg1=3' \  
  4. --data-urlencode 'arg2=8' \  
  5. --data-urlencode 'arg3=5' \  
  6. --data-urlencode '$scenario={"name":"invoke-operation","state":"input","data":{}}'  

OKボタン押下と同等の上記curlの結果、以下のHTMLが出力されました。

  1. <!DOCTYPE html>  
  2. <html><head>  
  3.     <title>Script Success</title>  
  4.     </head><body>  
  5.     <h1>Script Success</h1>  
  6.     43  
  7. </body></html>  

c:modelタグの場所に、スクリプト「(+ 3 (* 8 5))」の計算結果である43が出力されています。

まとめ

今回はフォーム画面から入力したパラメタを引数にしてCozy Scriptを実行する方法について説明しました。

次回はCozy Scriptに組み込まれたモデル駆動の機能を使用してリソースに対するアクセスを実現する方法について説明する予定です。

諸元

Cozy
0.0.9