以前、Mavenを使ってStruts2.2アプリを作りましたが、今回は同じ事をSeasar2でやってみました。
DIやLog4j、S2JDBCは追々やるとして、まずはMavenでSeasar2の雛形を作りました。
開発環境は前回記載したものを使います。
Javaの開発環境構築
プロジェクト作成
コマンドプロンプト(cmd)
cd C:¥work¥workspace
mvn archetype:generate -DarchetypeRepository=https://www.seasar.org/maven/maven2-snapshot/ \
-DarchetypeGroupId=org.seasar.sastruts \
-DarchetypeArtifactId=sa-struts-archetype -DarchetypeVersion=1.0.4-sp7.1-SNAPSHOT \
-DgroupId=ap -DartifactId=seasar2 -Dversion=1.0-SNAPSHOT
Y: : Y
※「[INFO] BUILD SUCCESS」と表示されればOK
Eclipseプロジェクト作成
コマンドプロンプト(cmd)
cd C:¥work¥workspace¥seasar2 mvn eclipse:eclipse -Dwtpversion=2.0
※「[INFO] BUILD SUCCESS」と表示されればOK
WARファイルインポート
コマンドプロンプト(cmd)
mvn war:inplace
※「[INFO] BUILD SUCCESS」と表示されればOK
Eclipseにインポート
Eclipseを起動して[ファイル][インポート]
インポート・ソース:Maven -> Existing Maven Projects Root Directory:/work/workspace/seasar2
エラーと警告を解消
下のマーカーに出るエラーと警告を解消して行きます。
[警告]Classpath entry org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER will not be exported or published. Runtime ClassNotFoundExceptions may result.
プロジェクト名を右クリックして[プロパティー]
Deployment Assembly -> Add -> Java Build Path Entries -> Maven Dependencies
[警告]ビルド・パスは実行環境 J2SE-1.5 を指定しています。
プロジェクト名を右クリックして[プロパティー]
Javaのビルド・パス -> ライブラリー:J2SE-1.5 -> JavaSE-1.6
[エラー]Java compiler level does not match the version of the installed Java project facet.
プロジェクト名を右クリックして[プロパティー]
Project Facets -> Java:1.5 -> 1.6
[警告]There is no schema defined for this pom.xml. Code completion will not work without a schema defined.
[警告]No grammar constraints (DTD or XML schema) detected for the document.
/pom.xmlを変更
<?xml version="1.0" encoding="UTF-8"?><project><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <省略> </project>
[エラー]Validation Message
無視(すべてを選択削除)
動作確認
プロジェクトを右クリック -> デバッグ -> Debug on Server
http://localhost:8080/seasar2/
致命的: サーブレット jsp のServlet.service()が例外を投げました org.apache.jasper.JasperException: JSPのクラスをコンパイルできません:
上記のエラーが出るがpom.xmlに不要なものがあるのが原因なので、こいつを削除する。
これに気付かず結構ハマった。
/pom.xmlを変更
<!-- If you want to use J2EE 1.4 server(not Tomcat),
add geronimo-jta_1.1_spec-1.0-tsr.jar -->
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jsp_2.0_spec</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_2.4_spec</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
http://localhost:8080/seasar2/
※「Hello!」と表示されればOK
おまけ
アプリ名を設定しておきます。
こいつはデプロイ後にTomcat Managerに表示されます。
/src/main/webapp/WEB-INF/web.xml
<省略>
<web-app
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>Struts Blank Convention</display-name>
<省略>
あと、nameとsa-strutsのバージョンをプロパティに定義しておきます。
/pom.xmlを変更
<省略>
<packaging>war</packaging>
<name>Seasar 2 Blank Convention Webapp</name>
<properties>
<sa-struts.version>1.0.4-sp7</sa-struts.version>
</properties>
<省略>
<dependency>
<groupId>org.seasar.sastruts</groupId>
<artifactId>sa-struts</artifactId>
<version>1.0.4-sp7</version>
<version>${sa-struts.version}</version>
</dependency>
<省略>
