Spring- MVC
Spring Web MVCフレームワークは、人気のあるSpringフレームワークのモジュールであり、Model-View-Controllerアーキテクチャに従っています。このフレームワークは、MVCデザインパターンにおけるコンポーネントの疎結合とともに、Springフレームワークの利点を取り入れたWebアプリケーションの作成を可能にします。HTTPリクエストとレスポンスを処理し、リクエストを必要なハンドラーとコントローラーにディスパッチするDispatcherServletを中心に構築されています。
この例では、Spring MVCフレームワークでサンプルアプリを構築し、WARファイルとしてエクスポートし、AppSailにデプロイする手順を説明します。
-
任意のIDEを使用して、新しいMavenプロジェクトを作成します。プロジェクトに以下のArchetypeを選択する必要があります:org.apache.maven.archetypes maven-archetype-webapp。
-
プロジェクトのpom.xmlファイルに、以下のSpring依存関係を追加します。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.19.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.19.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.19.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.19.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.19.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
- WARファイルをコンパイルおよび生成するために、以下の依存関係をbuildエンクロージャに追加します。
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
- プロジェクトにSpring XML設定ファイルを作成し、以下のBean設定を追加します。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
Spring設定ファイルは以下のようになります:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<context:component-scan base-package="com.amelia.example.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
-
プロジェクトにサーバーランタイムを追加します。例えば、Eclipse IDEで作業している場合、以下の方法でApache Tomcatサーバーのターゲットランタイムを追加できます:プロジェクトを右クリック → Properties → Targeted Runtime。ここで必要なランタイムを追加し、Apply をクリックします。
-
必要なコントローラーとエラーページ処理、およびプロジェクトのweb.xmlファイルにサーブレットマッピングを追加できます。例えば、以下の設定を使用できます:
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>helloworld</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>helloworld</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>401</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>400</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>408</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>413</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>414</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>505</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>405</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>409</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<exception-type>
java.lang.Throwable
</exception-type>
<location>/error.jsp</location>
</error-page>
-
アプリケーションをWARファイルとしてエクスポートして、Catalystにアップロードできます。Eclipseの例では、プロジェクトを右クリックし、Export As -> WAR を選択します。以下を行う必要があります:
a. ファイル名をROOT.warとして保存先を選択します
b. Optimize for specific server runtimeオプションのチェックを外します。
-
CLIから同じディレクトリでAppSailサービスを初期化するか、既存のプロジェクトディレクトリに追加できます。アプリのソースはアプリケーションのディレクトリである必要があります。アプリサービスの初期化時に以下の値を指定します:
Stack: Java 8
Platform: Java WAR -
初期化時に指定したビルドディレクトリにコンパイル済みファイルが追加されていることを確認します。Catalystは、リモートコンソールへのデプロイ時にアプリファイルを自動的にZIP圧縮します。
-
コンソールにアプリサービスをデプロイします。
デプロイされたアプリサービスには、そのエンドポイントURLからアクセスできます。
最終更新日 2026-02-23 18:09:41 +0530 IST
Yes
No
Send your feedback to us