Spring-MVC初めの一歩

Spring-MVCで画面を表示させるところまでを勉強したのでまとめます。

Spring3入門の6章の前半をまとめた感じです。

Spring tool suiteのインストール

http://www.springsource.org/downloads/sts-ggts
からインストーラをダウンロードする。
今回使用したのはlinuxの64bit版
spring-tool-suite-3.2.0.RELEASE-e3.8.2-linux-gtk-x86_64-installer.sh

Mavenプロジェクトを作成する

[File] > [New] > [Project]
[Maven] > [Maven Project] を選択

[Create a simple project(skip archetype selection)にチェックを付ける

以下の内容を入力する

Group Id: SampleGroup
Artifact Id: SampleArtifact
Packaging: war

これで空のプロジェクトが作成された。

web.xmlを作成する

作成するWebアプリケーションの基本的な設定を記述するためのweb.xmlを作成する。

src/main/webapp/WEB-INF/フォルダを作成し、そこに新たにweb.xmlというファイルを作成する。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	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_2_5.xsd">
	
	<!-- Webコンテナ上にDIコンテナを配置する設定...と書いてあった -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:/META-INF/spring/beans-biz.xml
		</param-value>
	</context-param>
	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
	
	<!-- DispatcherServletの定義 -->
	<servlet>
		<servlet-name>dispatcherServlet</servlet-name>
		<servlet-class>
			org.springframework.web.servlet.DispatcherServlet
		</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>
				classpath:/META-INF/spring/beans-webmvc.xml  <!-- ViewResolverなどの設定 -->
			</param-value>
		</init-param>
	</servlet>
	
	<!-- Mappingの設定 すべてのアクセスをDispatcherServletに渡してDispatcherServlet側で処理してもらう -->
	<servlet-mapping>
		<servlet-name>dispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

beans-webmvc.xmlを作成する

src/main/resources/META-INF/spring/beans-webmvc.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:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	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-3.1.xsd
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
		<!-- リクエストが来た際、このパッケージ内の@Controllerクラスの中から呼び出し先を探す -->
		<context:component-scan base-package="sample.controller" />

		<!-- @Controllerアノテーションを有効にするために必要 -->
		<mvc:annotation-driven/> 
		
		<!-- /WEB-INF/resources/以下のファイルをデプロイする -->
		<mvc:resources location="/WEB-INF/resources/" mapping="/resources/**"/>
		
		<!-- ViewResolverの設定 -->
		<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
			<property name="prefix" value="/WEB-INF/views/"/>
			<property name="suffix" value=".jsp"/>
		</bean>
</beans>

beans-biz.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"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>

spring-webmvcを追加する

pom.xmlを開く

Dependenciesタブを開く

[Dependencies]の[Add...]ボタンをクリック(左側のやつだよ)

以下の内容を入力する

Group Id: org.springframework
Artifact Id: spring-webmvc
Version: 3.1.1.RELEASE
Scope: compile

コントローラクラスを作成する

コントローラは、ブラウザからのリクエストを受け取り、適切な処理(DBからデータを引っ張ってきて加工するとかそういう処理)をして、必要なデータをまとめてViewに渡す役割

SpringMVCの場合、あらかじめアノテーションしておくことでURIとHTTPメソッドからDispatcherServletが呼び出すべきメソッドを指定する

package sample.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class SampleController {
	@RequestMapping(value="/sample", method=RequestMethod.GET)
	public String sample() {
		return "sample-view";
	}
}

jspを作成する

src/main/webapp/WEB-INF/views/
内にsample-view.jspを作成する。
今回はJSPの説明が目的ではなくて、とりあえずページが表示出来ればそれでいいので内容は以下にしておく。

Hello, Spring-MVC!

以上で作業は完了。

[Run As...] > [Maven build]

して、http://localhost:8080/SampleArtifact/sample にアクセスすると
"Hello, Spring-MVC!"って表示されるはず。


終わりに

XMLの設定が色々あってわかりにくかったので、最低限なにが必要でそれらはなにをしているのかを知るために行った作業のログです。