このチュートリアルでは、

Google App Engine

(GAE)環境で

Struts 2

Webアプリケーションを開発する方法を説明します。

使用されるツールと技術:

  1. JDK 1.6

  2. Eclipse用Eclipse 3.7 Google Plugin

  3. Google App Engine Java SDK 1.6.3.1

  4. Struts 2.3.1.2

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

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


gae struts2の例new project、title = "gae-struts2-example1"、width = 360、height = 479

Google Plugin for Eclipseは、GAEプロジェクト構造のサンプルを生成します。後で、この生成されたGAEプロジェクトにStruts2を統合する方法を説明します。


gae struts2サンプルサンプルプロジェクト、title = "gae-struts2-example2-sample-project"、width = 459、高さ= 358

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

以下のStruts 2依存ライブラリ、http://struts.apache.org/download.cgi[ここでStruts2をダウンロード]を入手してください。

  • 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-example3-library」、width = 521、height = 645]

プロジェクトフォルダを右クリックし、 ”

Properties

” – > ”

Java Build Path

” – > ”

Libraries

“タブを選択し、 ”

Add Jars

“ボタンをクリックし、 ”

war/WEBビルドパスに「-INF/lib

」フォルダを作成します。


gae struts2の例のJava構築パス、title = "gae-struts2-example4-build-path"、width = 629 、高さ= 480

3. Struts 2コードの統合

3.1生成された `Struts2GoogleAppEngineServlet.java`を削除します。これは必要ありません。

3.2新しいStruts 2 Actionクラスを作成します。


File:src/com/mkyong/user/action/WelcomeUserAction.java

package com.mkyong.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リスナー・クラスを作成し、ognl securityをnullに設定します。

  • Note ** + Struts 2は、このリスナーがGAE環境で動作するために必要です。これを読んでください –

    GAEにStruts 2をデプロイする際の問題

    とリンク://google-app-engine/strurts- 2-on-gae-error-result-null-not-found/[エラー:

結果 ‘null’が見つかりません]


File:src/com/mkyong/listener/Struts2ListenerOnGAE.java

package com.mkyong.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ローカルGAE環境でStruts2プロジェクトを実行するには、

TextBlock`クラスを作成して元の

TextBlok`クラスをオーバーロードする必要があります。そうしないと、 “ javax.swing.tree.TreeNodeは制限されたクラスです。 Struts2チームが今後これを修正できることを期待しています。

  • TextBlockソースコード** リンク://google-app-engine/javax-swing-tree-treenode-is-a-restricted-class/[ダウンロードTextBlockソースコード]

3.5プロジェクトのディレクトリ構造を見直します。

4. Struts 2ページの統合

4.1ユーザ入力を受け入れる `login.jsp`ページを作成します。


File:war/User/pages/login.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head></head>
<body>
<h1>GAE + Struts 2 Example</h1>

<s:form action="Welcome">
    <s:textfield name="username" label="Username"/>
    <s:password name="password" label="Password"/>
    <s:submit/>
</s:form>

</body>
</html>

4.2 `welcome__user.jsp`ページを作成します。


File:war/User/pages/welcome

user.jsp__

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head></head>
<body>
<h1>GAE + Struts 2 Example</h1>

<h2>Hello <s:property value="username"/></h2>

</body>
</html>

4.3プロジェクトのディレクトリ構造を再度確認します。


gae struts2サンプルディレクトリ、title = "gae-struts2-example6-directory"、width = 463、height = 342

5. Struts XML設定

`struts.xml`ファイルを作成し、「

src/struts.xml

」に入れてください。

ファイル:struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

  <package name="user" namespace="/User" extends="struts-default">
    <action name="Login">
        <result>pages/login.jsp</result>
    </action>
    <action name="Welcome" class="com.mkyong.user.action.WelcomeUserAction">
        <result name="SUCCESS">pages/welcome__user.jsp</result>
    </action>
   </package>

</struts>

6. web.xml

`web.xml`を更新し、Struts2を統合し、ognlセキュリティリスナを設定します。


File:web.xml

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app__2__5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app__2__5.xsd"
    version="2.5">
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/** </url-pattern>
    </filter-mapping>

    <listener>
        <listener-class>
                   com.mkyong.listener.Struts2ListenerOnGAE
                </listener-class>
    </listener>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

7.ディレクトリ構造

最終的なディレクトリ構造を確認してください。

8.ローカルで実行する

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

Web Application

」として実行します。


URL:http://localhost:8888/User/Login.action

「gae-struts2-example8-run-local」、width = 577 、高さ= 349]


gae struts2の例、ローカルで実行、title = "gae-struts2-example8-run-local-2" 、幅= 577、高さ= 349

9. GAEに展開する

`appengine-web.xml`を更新し、あなたのApp EngineアプリケーションIDを入れてください。

<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
  <application>mkyong-struts2gae</application>
  <version>1</version>

  <!-- Configure java.util.logging -->
  <system-properties>
    <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
  </system-properties>

</appengine-web-app>

プロジェクトを選択し、Googleのアイコン「

App Engineへのデプロイ

」をクリックします。 Google Plugin for Eclipseは、すべての必要なファイルをGAEのプロダクションに自動的に配備します。


gae struts2例をGAEにデプロイする、title = "gae-struts2-example9-deploy-gae"、width = 549 、高さ= 476

デプロイ時に、Eclipseコンソールビューに同様のメッセージが表示されます。

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

展開の準備:

ステージングディレクトリを 'C:¥Users¥mkyong¥AppData¥Local¥Temp¥appcfg7432687551.tmp'に作成しました.JSPファイルをスキャンしています。

jspファイルのコンパイル。

ローカルディスク上のファイルのスキャン。

更新の開始。

2つの静的ファイルのクローン作成。

46個のアプリケーションファイルのクローン作成。

展開:

12のファイルをアップロードしています。

3つのファイルをアップロードしました。

6件のファイルをアップロードしました。

9個のファイルをアップロードしました。

12のファイルをアップロードしました。

プリコンパイルの初期化中...

合計44KBの11個のファイルを含むバッチを送信しています。

合計1KBの1つのBLOBを含むバッチを送信しています。

新しいバージョンを導入する。

可用性の確認:

1秒後にもう一度チェックします。

2秒後にもう一度チェックします。

4秒後にもう一度チェックします。

更新の終了:新しいバージョンのサービスを開始する準備が整いました。

データストアの更新:

インデックス定義のアップロード。

展開が正常に完了しました


gae struts2の実行例 GAE、title = "gae-struts2-example10-deploy-gae"、width = 583、height = 357





最後に、この長い記事を完成させました。総合的な統合はあまりありません
Struts2 ognlセキュリティとTextBlockの問題を修正するだけで、
Struts2のチームが将来この問題を解決できることを願っています。

===ソースコードをダウンロードする

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

ダウンロード –

Struts2GoogleAppEngine.zip

(23 KB)

===参考文献

。リンク://struts2/struts-2-hello-world-example/[Struts2 hello world
例]。

Google
App Engine + JavaのHello worldの例、Eclipseを使用



Apache Struts



Struts
2 on GAE


gae


struts2