2012年12月9日日曜日

Using Javascript / Javascriptを使用する

Prepare
  1. Download patchJavascript addon from http://sourceforge.jp/projects/ofbiz-jp/downloads/57320/patchJavascript-0.1.0-v11.04.01.zip and install.
  2. Rebuild java source by using ant command (no options).

Example
  1. In this example, modify Jython sample. see http://ofbizj.blogspot.jp/2012/12/jython-script.html
  2. In directory hot-deploy/helloworld/script/org/ofbiz/test, create file JavaScriptTest.js and edit like bellow.

    importPackage(java.lang);
    importPackage(java.util);

    response = new HashMap();
    response.put("result", "OK");
    bsf.registerBean("response", response)
    System.out.println("Hello !");

  3. Edit file hot-deploy/helloworld/servicedef/service.xml like bellow.

    <services xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nonamespaceschemalocation="http://ofbiz.apache.org/dtds/services.xsd">
        <description>OFBIZ Test Services</description>
        <vendor>OFBiz</vendor>
        <version>1.0</version>

        <service auth="false" engine="jython" invoke="" location="component://helloworld/script/org/ofbiz/test/JythonScriptTest.py" name="testJythonScript">
            <description>Test JythonScript Service</description>
            <attribute mode="OUT" name="result" type="String">
        </attribute></service>
        <service auth="false" engine="javascript" invoke="" location="component://helloworld/script/org/ofbiz/test/JavaScriptTest.js" name="testJavaScript">
            <description>Test JavaScript Service</description>
            <attribute mode="OUT" name="result" type="String">
        </attribute></service>
    </services>

  4. Edit hot-deploy/helloworld/webapp/helloworld/WEB-INF/controller.xml like bellow.

        <request-map uri="main">
           <security https="false" auth="false"/>
           <event type="service" invoke="testJavaScript"/> <= edit this line
           <response name="success" type="view" value="main"/>
        </request-map>

  5. see http://localhost:8080/helloworld/control/main page using browser and see logs. Browser displays "helloworld" and in logs shows "Hello !".


準備
  1. patchJavascript アドオンを次の場所からダウンロードします http://sourceforge.jp/projects/ofbiz-jp/downloads/57320/patchJavascript-0.1.0-v11.04.01.zip 。その後、adm install patchJavascript-0.1.0-v11.04.01コマンドでインストールします。
  2. Javaソースがパッチで変更されるのでantコマンドで再ビルドします。antコマンドのオプションは不要です。OFBIZ_HOMEディレクトリに移動してから実行してください。

実行
  1. patchJythonのサンプルを使用しますのであらかじめ準備してください http://ofbizj.blogspot.jp/2012/12/jython-script.html
  2. hot-deploy/helloworld/script/org/ofbiz/test ディレクトリに JavaScriptTest.js ファイルを次の内容で作成します。

    from java.util import HashMap
    response = HashMap()
    response.put("result", "OK")
    bsf.registerBean("response", response)
    print "Hello !"

  3. hot-deploy/helloworld/servicedef/service.xml ファイルを次の内容に編集します。

    <services xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nonamespaceschemalocation="http://ofbiz.apache.org/dtds/services.xsd">
        <description>OFBIZ Test Services</description>
        <vendor>OFBiz</vendor>
        <version>1.0</version>

        <service auth="false" engine="jython" invoke="" location="component://helloworld/script/org/ofbiz/test/JythonScriptTest.py" name="testJythonScript">
            <description>Test JythonScript Service</description>
            <attribute mode="OUT" name="result" type="String">
        </attribute></service>
        <service auth="false" engine="javascript" invoke="" location="component://helloworld/script/org/ofbiz/test/JavaScriptTest.js" name="testJavaScript">
            <description>Test JavaScript Service</description>
            <attribute mode="OUT" name="result" type="String">
        </attribute></service>
    </services>

  4. hot-deploy/helloworld/webapp/helloworld/WEB-INF/controller.xml ファイルを変更します。

        <request-map uri="main">
           <security https="false" auth="false"/>
           <event type="service" invoke="testJavaScript"/> <= edit this line
           <response name="success" type="view" value="main"/>
        </request-map>

  5. 次のURLをブラウザで表示します http://localhost:8080/helloworld/control/main 。画面には "helloworld" が表示されて、ログには "Hello !" が表示されて、サービスが呼び出されたことが確認できます。




0 件のコメント:

コメントを投稿