Google App Engine Struts 1の例

Google App Engine + Struts 1の例

このチュートリアルでは、長生きする古典的なStruts 1フレームワークで、Google App Engine(GAE)環境でStruts 1.xWebアプリケーションを開発する方法を示します。

使用されるツールとテクノロジー:

  1. JDK 1.6

  2. Eclipse 3.7 + Eclipse用Googleプラグイン

  3. Google App Engine Java SDK 1.6.3.1

  4. Struts 1.3.10

Note
このGoogle App Engine + Struts 2 exampleにも興味があるかもしれません。

この例では、Struts 1.x hello world exampleをこのGAE + Java exampleとマージします。

1. 新しいWebアプリケーションプロジェクト

Eclipseで、「StrutsGoogleAppEngine」という名前の新しいWebアプリケーションプロジェクトを作成します。

struts1 on gae example

Eclipse用Googleプラグインは、サンプルのGAEプロジェクト構造を生成します。 後でStruts 1をこのGAE構造に統合します。

2. Struts 1.xライブラリの統合

download Struts 1.xへのこのリンクにアクセスしてください。 次のjarが必要です。

  • antlr-2.7.2.jar

  • commons-beanutils-1.8.0.jar

  • commons-chain-1.2.jar

  • commons-digester-1.8.jar

  • commons-logging-1.0.4.jar

  • commons-validator-1.3.1.jar

  • oro-2.0.8.jar

  • struts-core-1.3.10.jar

  • struts-taglib-1.3.10.jar

コピーして「war/WEB-INF/lib」フォルダに入れます。

struts1 on gae example library

プロジェクトフォルダを右クリックし、「Properties」を選択します。 「Java Build Path」→「Libraries」タブを選択し、「Add Jars」ボタンをクリックして、「war/WEB-INF/lib」フォルダーからビルドパスに9つ以上のjarファイルを選択します。

struts1 on gae example java build

3. Struts 1.xアクションとフォームを統合する

3.1 Delete StrutsGoogleAppEngineServlet.java , you don’t need this.

3.2 Create a new Action class.

ファイル:src / com / example / action / HelloWorldAction.java

package com.example.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.example.form.HelloWorldForm;

public class HelloWorldAction extends Action {

    public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {

        HelloWorldForm helloWorldForm = (HelloWorldForm) form;
        helloWorldForm.setMessage("Hello World!");

        return mapping.findForward("success");
    }

}

3.3 Create a new form class.

ファイル:src / com / example / form / HelloWorldForm.java

package com.example.form;

import org.apache.struts.action.ActionForm;

public class HelloWorldForm extends ActionForm {

    String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

}

4. Struts 1.x JSPページの統合

4.1 Create HelloWorld.jsp page and put in “war/User/pages/HelloWorld.jsp“.

ファイル:HelloWorld.jsp

<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>





    

Google App Engine + Struts 1.x example

5. Struts XML構成

struts-config.xmlファイルを作成し、「war/WEB-INF/struts-config.xml」に入れます。

ファイル:struts-config.xml





    
       
    

    
       
        

       
    

6. web.xml

web.xmlを更新し、Strutsを統合します。

ファイル:web.xml



    
        action
        org.apache.struts.action.ActionServlet
        
            config
            
                /WEB-INF/struts-config.xml
                
        
        1
    

    
        action
        *.do
    

    
        index.html
    

7. GAEでセッションを有効にする

appengine-web.xmlを更新し、セッションサポートを有効にします。Strutsにはこれが必要です。

ファイル:appengine-web.xml


  
  1

    true

Note
GAEでセッションを有効にしないと、エラー「java.lang.RuntimeException: Session support is not enabled in appengine-web.xml」が発生します。

8. ディレクトリ構造

最終的なディレクトリ構造を確認します。

struts1 on gae example final directory structure

9. ローカルで実行

プロジェクトを右クリックし、「Web Application」として実行します。

struts1 on gae example run on local

10. GAEにデプロイする

appengine-web.xmlファイルを更新し、AppEngineアプリケーションIDを追加します。

ファイル:appengine-web.xml


  example-strutsgae
  1

  true

プロジェクトを選択し、Googleアイコン「Deploy to App Engine」をクリックします。

struts1 on gae example deploy on gae

Note
それほど問題はありません。GAEディレクトリ構造に従ってください。少なくとも、Struts1はStruts2よりも簡単に統合できます。

ソースコードをダウンロード

ファイルサイズが大きいため、すべてのStruts1 jarは除外されます。手動でダウンロードする必要があります。

ダウンロード–StrutsGoogleAppEngine(13 KB)