WTPでMaven2を使ったStruts2プロジェクトの作成手順
今回はEclipseのみを使ってStruts2を使用したWebアプリケーションのMavenプロジェクトを作成していきます。
手順は以下のような流れでやっていきます。
プロジェクトの作成
- メニューからFile > New > Projectを選択。
- プロジェクトのカテゴリーからWeb > Dynamic Web Projectを選択。
- プロジェクト名を記入し、(基本その他設定はデフォルトでOK)Nextを押して行き、最後にFinishでプロジェクト作成は終了。
Mavenの有効化
- プロジェクトを右クリックし、MavenEnable > Dependency Managementを選択。
Create new POMでは次のように設定
- Group Id > まとめたいグループ名を記述
- Artifact Id > アプリケーション名を記述
- Version > 今回作成するアプリのバージョンを記述(とりあえずデフォルトでOK)
- Packaging > Webアプリなのでwarを選択
pom.xmlに設定追加
Mavenの有効化をするとプロジェクト直下にpom.xmlが作成されているのでStruts2の依存設定を追加。
<?xml version="1.0" encoding="UTF-8"?><project> <modelVersion>4.0.0</modelVersion> <groupId>hello-Struts2</groupId> <artifactId>hello-Struts2</artifactId> <name></name> <version>0.0.1-SNAPSHOT</version> <description></description> <!-- 追加 ここから --> <dependencies> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.0.11.1</version> </dependency> </dependencies> <!-- 追加 ここまで --> </project>
ソースディレクトリの作成
- メニューのWindow > Show view > Navigatorでナビゲータパースペクティブを開く
- Mavenの[http:/maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html:itle=標準ディレクトリ構成]にしたがってディレクトリを作成
src/main/java src/main/resources src/test/java src/test/resources
ビルドパスの設定
- プロジェクト右クリック > Java Build PathメニューのSourceタブを選択
- デフォルトに設定されているsrcをRemove
- 先ほど作成したフォルダをAdd Folderで追加
.classpathファイルがこんな感じになります。
変更前
<?xml version="1.0" encoding="UTF-8"?> <classpath> <classpathentry kind="src" path="src"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> <classpathentry kind="con" path= "org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v5.5"/> <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/> <classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/> <classpathentry kind="output" path="build/classes"/> </classpath>
変更後
<?xml version="1.0" encoding="UTF-8"?> <classpath> <classpathentry kind="src" path="src/main/resources"/> <classpathentry kind="src" path="src/main/java"/> <classpathentry kind="src" path="src/test/resources"/> <classpathentry kind="src" path="src/test/java"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> <classpathentry kind="con" path= "org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v5.5"/> <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/> <classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/> <classpathentry kind="output" path="build/classes"/> </classpath>
設定ファイルの追加、修正
src/resources/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> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" /> </struts>
src/resources/MessageResources.propertiesの追加
ひとまずアプリケーション全体で使用するメッセージプロパティでいいのでMessageResources.propertiesを作成し(中身は空っぽでOK)、struts.xmlに次の行を追加
<constant name="struts.custom.i18n.resources" value="MessageResources" />
src/resources/log4j.propertiesの追加
# # Log4J Settings for log4j 1.2.x (via jakarta-commons-logging) # # The five logging levels used by Log are (in order):n# # 1. DEBUG (the least serious) # 2. INFO # 3. WARN # 4. ERROR # 5. FATAL (the most serious) # Set root logger level to WARN and append to stdout log4j.rootLogger=WARN,stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout # Pattern to output the caller's file name and line number. log4j.appender.stdout.layout.ConversionPattern=%d %5p (%c:%L) - %m%n # Print only messages of level ERROR or above in the package noModule. log4j.logger.noModule=FATAL # OpenSymphony Stuff log4j.logger.com.opensymphony=INFO log4j.logger.org.apache.struts2=DEBUG # Spring Stuff log4j.logger.org.springframework=INFO
web.xmlの編集
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" 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"> <display-name> hello-Struts2</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
関連書籍
Practical Apache Struts 2 Web 2.0 Projects (Practical Projects)
posted with amazlet at 08.08.07
Ian Roughley
Apress
売り上げランキング: 12492
Apress
売り上げランキング: 12492
Apache Maven 2.0入門 Java・オープンソース・ビルドツール
posted with amazlet at 08.08.07