長いStruts 1の古典的なフレームワークをこのチュートリアルでは、
Google App Engine
(GAE)環境で
Struts 1.x
Webアプリケーションを開発する方法を紹介します。
使用されるツールと技術:
-
JDK 1.6
-
Eclipse用Eclipse 3.7 Google Plugin
-
Google App Engine Java SDK 1.6.3.1
-
Struts 1.3.10
この例では、
Struts 1.x hello world example
をこのリンクとマージします://google-app-engine/google-app-engine-hello-world -example-using-eclipse/[GAE Javaの例]。
1.新しいWebアプリケーションプロジェクト
Eclipseで、 “StrutsGoogleAppEngine”という名前の新しいWebアプリケーションプロジェクトを作成します。
Google Plugin for Eclipseは、サンプルのGAEプロジェクト構造を生成します。
後でこのGAE構造にStruts 1を統合します。
2. Struts 1.xライブラリの統合
download Struts 1.x
へのリンクを参照してください。
次のジャーが必要です:
-
antlr-2.7.2.jar
-
コモンズ – 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
“フォルダにコピーして入れてください。
プロジェクトフォルダを右クリックし、 ”
Properties
“を選択します。 ”
Java Build Path
” – > ”
Libraries
“タブを選択し、 ”
Add Jars
“ボタンをクリックし、ビルドパスに “war/WEB-INF/lib ** “フォルダから9ジャーを選択します。
3. Struts 1.xのアクションとフォームを統合する
3.1 `StrutsGoogleAppEngineServlet.java`を削除します。これは必要ありません。
3.2新しいActionクラスを作成します。
ファイル:src/com/mkyong/action/HelloWorld Action.java
package com.mkyong.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.mkyong.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新しいフォームクラスを作成します。
File:src/com/mkyong/form/HelloWorldForm.java
package com.mkyong.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 `HelloWorld.jsp`ページを作成し、”
war/User/pages/HelloWorld.jsp
“に入れます。
File:HelloWorld.jsp
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
</head>
<body>
<h1>
Google App Engine + Struts 1.x example
</h1>
<h2><bean:write name="helloWorldForm" property="message"/></h2>
</body>
</html>
5. Struts XML設定
`struts-config.xml`ファイルを作成し、それを”
war/WEB-INF/struts-config.xml
“に入れてください。
ファイル:struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://jakarta.apache.org/struts/dtds/struts-config__1__3.dtd">
<struts-config>
<form-beans>
<form-bean name="helloWorldForm" type="com.mkyong.form.HelloWorldForm"/>
</form-beans>
<action-mappings>
<action path="/helloWorld" type="com.mkyong.action.HelloWorldAction"
name="helloWorldForm">
<forward name="success" path="/HelloWorld.jsp"/>
</action>
</action-mappings>
</struts-config>
6. web.xml
`web.xml`を更新し、Strutsを統合します。
ファイル: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">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>
/WEB-INF/struts-config.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>** .do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
7. GAEでセッションを有効にする
`appengine-web.xml`を更新し、セッションのサポートを有効にして、Strutsがこれを必要とします。
ファイル:appengine-web.xml
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application></application>
<version>1</version>
<sessions-enabled>true</sessions-enabled>
</appengine-web-app>
-
Note ** + GAEでセッションを有効にしないと、エラー ”
java.lang.RuntimeException:セッションのサポートがappengine-web.xml
で有効になっていません”が表示されます。
8.ディレクトリ構造
最終的なディレクトリ構造を確認してください。
9.ローカルで実行する
プロジェクトを右クリックし、「
Web Application
」として実行します。
URL:http://localhost:8888/helloWorld.do
10. GAEに展開する
`appengine-web.xml`ファイルを更新し、あなたのApp EngineアプリケーションIDを追加してください。
File:appengine-web.xml
<?xml version="1.0" encoding="utf-8"?> <appengine-web-app xmlns="http://appengine.google.com/ns/1.0"> <application>mkyong-strutsgae</application> <version>1</version> <sessions-enabled>true</sessions-enabled> </appengine-web-app>
プロジェクトを選択し、Googleのアイコン「
App Engineへのデプロイ
」をクリックします。
URL:http://mkyong-strutsgae.appspot.com/helloWorld.do
-
注意** あまり問題はなく、GAEのディレクトリ構造に従ってください。少なくとも、Struts 1はStruts 2より簡単です。
ソースコードをダウンロードする
ファイルサイズが大きいため、Struts1のすべてのjarファイルは除外されているため、手動でダウンロードする必要があります。
ダウンロード:
StrutsGoogleAppEngine
(13 KB)
参考文献
-
link://google-app-engine/google-app-engine-hello-world-example-using-eclipse/[Google
App Engine JavaのHello worldの例、Eclipseを使用]。
Struts 1.xのダウンロード