Google App Engine Struts 2の例

Google App Engine + Struts 2の例

このチュートリアルでは、Google App Engine(GAE)環境でStruts 2Webアプリケーションを開発する方法を示します。

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

  1. JDK 1.6

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

  3. Google App Engine Java SDK 1.6.3.1

  4. Struts 2.3.1.2

Note
このチュートリアルに進む前に、必ずこれを読んでください–GAE + Java exampleおよびStruts 2 hello world example

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

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

gae struts2 example new project

Eclipse用Googleプラグインは、GAEプロジェクト構造のサンプルを生成します。 後で、Struts2をこの生成されたGAEプロジェクトと統合する方法を示します。

gae struts2 example sample project

2. Struts 2ライブラリを統合する

次のStruts2依存関係ライブラリdownload Struts2 hereを取得します。

  • asm-3.3.jar

  • asm-commons-3.3.jar

  • asm-tree-3.3.jar

  • commons-fileupload-1.2.2.jar

  • commons-io-2.0.1.jar

  • commons-lang-2.5.jar

  • freemarker-2.3.18.jar

  • javassist-3.11.0.GA.jar

  • ognl-3.0.4.jar

  • struts2-core-2.3.1.2.jar

  • xwork-core-2.3.1.2.jar

すべてを「war/WEB-INF/lib」フォルダに入れます。

gae struts2 example libraries

プロジェクトフォルダを右クリックし、「Properties」→「Java Build Path」→「Libraries」タブを選択し、「Add Jars」ボタンをクリックして、「%」から11個以上のjarファイルを選択します。 (t4)s」フォルダーをビルドパスに追加します。

gae struts2 example java build path

3. Struts 2コードを統合する

3.1 Delete the generated Struts2GoogleAppEngineServlet.java, you don’t need this.

3.2 Create a new Struts 2 Action class.

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

package com.example.user.action;

public class WelcomeUserAction {

    private String username;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String execute() {

        return "SUCCESS";

    }
}

3.3 Create a listener class, and set ognl security to null.

Note
Struts 2をGAE環境で実行するには、このリスナーが必要です。 これを読んでください–Issues when deploying Struts 2 on GAEError: result ‘null’ not found

ファイル:src / com / example / listener / Struts2ListenerOnGAE.java

package com.example.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import ognl.OgnlRuntime;

public class Struts2ListenerOnGAE implements ServletContextListener,
        HttpSessionListener, HttpSessionAttributeListener {

    public void contextInitialized(ServletContextEvent sce) {
        OgnlRuntime.setSecurityManager(null);
    }

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void sessionCreated(HttpSessionEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void sessionDestroyed(HttpSessionEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void attributeAdded(HttpSessionBindingEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent arg0) {
        // TODO Auto-generated method stub

    }

}

3.4 To run Struts2 project in local GAE environment, you have to create a TextBlock class and overload the original TextBlok class, otherwise you will hit “javax.swing.tree.TreeNode is a restricted class” error message. Struts2チームが将来のリリースでこれを修正できることを願っています。

TextBlock Source Code
このURLをdownload TextBlock source codeに移動します。

3.5 Review project directory structure.

gae struts2 example directory

4. Struts 2ページの統合

4.1 Create a login.jsp page, to accept user input.

ファイル:war / User / pages / login.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>



GAE + Struts 2 Example

4.2 Create a welcome_user.jsp page.

ファイル:war / User / pages / welcome_user.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>



GAE + Struts 2 Example

Hello

4.3 Review project directory structure again.

gae struts2 example directory

5. Struts XML構成

struts.xmlファイルを作成し、「src/struts.xml」に入れます。

ファイル:struts.xml





  
    
        pages/login.jsp
    
    
        pages/welcome_user.jsp
    
   

6. web.xml

web.xmlを更新し、Struts2を統合し、ognlセキュリティリスナーを構成します。

ファイル:web.xml


    
        struts2
        
        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        
    

    
        struts2
        /*
    

    
        
                   com.example.listener.Struts2ListenerOnGAE
                
    
    
        index.html
    

7. ディレクトリ構造

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

gae struts2 example final directory

8. ローカルで実行

完了、テストを実行する時間。 プロジェクトを右クリックし、「Web Application」として実行します。

gae struts2 example run on local

gae struts2 example run on local

9. GAEにデプロイする

appengine-web.xmlを更新し、AppEngineアプリケーションIDを入力します。


  example-struts2gae
  1

  
  
    
  

プロジェクトを選択し、Googleアイコン「Deploy to App Engine」をクリックします。 Eclipse用Googleプラグインは、必要なすべてのファイルをGAE運用環境に自動的にデプロイします。

gae struts2 example deploy to GAE

展開中、Eclipseコンソールビューに次の同様のメッセージが表示されます。

------------ Deploying frontend ------------

Preparing to deploy:
    Created staging directory at: 'C:\Users\example\AppData\Local\Temp\appcfg7432687551.tmp'
    Scanning for jsp files.
    Compiling jsp files.
    Scanning files on local disk.
    Initiating update.
    Cloning 2 static files.
    Cloning 46 application files.

Deploying:
    Uploading 12 files.
    Uploaded 3 files.
    Uploaded 6 files.
    Uploaded 9 files.
    Uploaded 12 files.
    Initializing precompilation...
    Sending batch containing 11 file(s) totaling 44KB.
    Sending batch containing 1 blob(s) totaling 1KB.
    Deploying new version.

Verifying availability:
    Will check again in 1 seconds.
    Will check again in 2 seconds.
    Will check again in 4 seconds.
    Closing update: new version is ready to start serving.

Updating datastore:
    Uploading index definitions.

Deployment completed successfully

URL:* http://example-struts2gae.appspot.com/User/Login.action *

gae struts2 example run on GAE

Note
最後に、この長い記事を終了しました。 全体的な統合はそれほど難しくありません。Struts2のognlセキュリティとTextBlockの問題を修正するだけで、Struts2のチームが将来これを修正できることを願っています。

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

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

ダウンロード–Struts2GoogleAppEngine.zip(23 KB)