開発環境:
apache-tomcat-8.5.15
jdk1.8.0_172
IntelliJ IDEA
Maven Webアプリケーションプロジェクトの作成: 新しいプロジェクトを作成する際、Create New Projectを選択し、次へをクリックします。
再度次へをクリックします。
デフォルトのMaven設定を使用することも可能で、リポジトリのパスを変更してダウンロード速度を向上させることができます。または、ローカルにインストールされたapache-maven-3.5.2を使用することもできます。
さらに次へをクリックします。
完了を選択します。
依存関係の追加 プロジェクトが生成された後のディレクトリ構造は以下のようになります。
pom.xmlファイルを開き、必要な依存関係を追加します。
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-springmvc-app</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<name>My Spring MVC Application</name>
<url>http://example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>myapp</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</build>
</project>
ディレクトリ構造の設定
プロジェクト作成後、src/main配下にjavaディレクトリを作成しても、その中にパッケージやJavaクラスを作成できません。IntelliJ IDEAではディレクトリを適切にマークする必要があります。
File->Project Structureを選択します。
対応するディレクトリがまだ作成されていない場合は、まずディレクトリを作成し、適切なマークを選択します:
- Sources:通常srcのようなコンパイル可能なディレクトリをマークするために使用されます。プロジェクトのsrcディレクトリだけでなく、他の特別なディレクトリもコンパイル可能にする必要がある場合、このディレクトリをマークする必要があります。SourcesとしてマークされたディレクトリのみがJavaクラスとパッケージを作成できます。
- Tests:通常ユニットテスト用のコンパイル可能なディレクトリをマークするために使用されます。標準的なMavenプロジェクト構造では、トップレベルディレクトリはsrcで、MavenのsrcはSourcesとして設定しませんが、そのサブディレクトリmain配下のjavaディレクトリをSourcesとして設定します。ユニットテスト用ディレクトリはsrc - test - javaであり、このjavaディレクトリをTestsとして設定し、ユニットテスト用のコンパイル可能ディレクトリであることを示します。
- Resources:通常リソースファイル用のディレクトリをマークするために使用されます。Mavenプロジェクトでは、リソースディレクトリが別途分けられており、そのディレクトリは:src - main -resourcesです。このresourcesディレクトリをResourcesとして設定し、リソースディレクトリであることを示します。リソースディレクトリ内のファイルは出力ディレクトリにコンパイルされます。 Test Resources:ユニットテスト用のリソースファイルディレクトリをマークするために使用されます。Mavenプロジェクトでは、ユニットテスト用のリソースディレクトリが別途分けられており、そのディレクトリは:src - test -resourcesです。このresourcesディレクトリをTest Resourcesとして設定し、ユニットテスト用のリソースディレクトリであることを示します。
- Excluded:通常除外ディレクトリをマークするために使用されます。除外されたディレクトリはIntelliJ IDEAによってインデックス化されず、IntelliJ IDEAによって無視されたとみなされます。
マーク付けが完了したら、以下のようなディレクトリ構造を作成します。
web.xmlの設定
Mavenによって自動生成されたweb.xmlを使用すると、Spring MVCが返すパラメータ値を読み込めない可能性があるため、web.xmlを修正する必要があります。
既存のファイルを直接編集します:
<web-app>
<display-name>My Maven Generated Web Application</display-name>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
新しいファイル:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>My Maven Generated Web Application</display-name>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
元のファイルのweb-app属性を置き換える必要があることに注意してください。
<web-app>
<display-name>My Maven Generated Web Application</display-name>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
起動時にデフォルトで/WEB-INFディレクトリ下からXXX-servlet.xmlを検索します。XXXはDispatcherServletの名前です。パスを手動で設定することもできますが、ここではデフォルト設定を使用します。
WEB-INFディレクトリ内にspring-config.xmlファイルを作成します:
spring-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.example.controllers"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
pagesディレクトリの追加:index.jspファイルをこのディレクトリに移動します。
コードパス/src/main/javaにcom.example.controllersパッケージを追加し、HomeController.javaを作成します:
package com.example.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HomeController {
@GetMapping(value = "/welcome")
public String welcomePage(Model model) {
model.addAttribute("message", "Welcome to Spring MVC");
return "index";
}
}
Tomcatサーバーの設定
Configure...でTomcatのパスを設定します。
緑色の+ボタンを押して、プロジェクトをデプロイメントリストに追加します。
サーバー名とTomcatバージョンを選択します。
再度緑色の+ボタンを押して、プロジェクトをデプロイメントリストに追加します。
2番目のオプションを選択することに注意してください。
最後にTomcatサービスを起動して実行し、結果を表示します。