Spring MVC 4.0 No. 001 Hello world
Spring MVCでHello worldを表示する最低限のプログラムです。
これ以降のサンプルの基本となる部分です。
Springに必要なライブラリーは依存関係が複雑で大変なのでMavenで導入するのが簡単です。
pom.xml
<dependencies> <!-- Spring Framework --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.0.6.RELEASE</version> </dependency> <!-- Servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies>
Springのライブラリーは、spring-webmvcを指定することで必要な物がひと通り揃います。後は、コンパイル用のServlet/JSPのAPIと汎用のJSTLタグライブラリーを入れておけばいいかと思います。
続いて、Deployment descriptorになります。Springに必要な設定をweb.xmlに記載します。
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/spring-context.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <jsp-config> <jsp-property-group> <url-pattern>*.jsp</url-pattern> <el-ignored>false</el-ignored> <page-encoding>utf-8</page-encoding> <scripting-invalid>true</scripting-invalid> <include-prelude>/WEB-INF/jsp/common/common.jsp</include-prelude> </jsp-property-group> </jsp-config> </web-app>
長いですが、おおよそ定型句です。
まずfilterですが、requestとresponseの文字コードの指定を毎回しなくていいようにCharacterEncodingFilterを設定します。使用する文字コードに合わせて設定してください。今どきはutf-8一択だと思います。
あとは、Springのリクエストを受け付けるためのDispatherServletの設定になります。DispatherSerlvetの設定で大事なのは、servlet-mappingとパラメータのcontextConfigLocationになります。servlet-mappingはここでは/としています。特定のURLにしたい場合には、/hogeや/fooとしてください。contextConfigLocationはSpring用の設定ファイルの場所を示します。複数ある場合には「,」区切りで指定します。
続いて、web.xmlの中で指定したcontextConfigLocationのファイルの中身を確認します。このファイルがSpringの設定の本体になります。
WEB-INF/spring/spring-context.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <mvc:annotation-driven /> <context:component-scan base-package="com.example.spring" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
<mvc-annotation-driven />でSpring MVCの設定が自動的に行われます。
component-scanで、Springのコンポーネントを検索するパッケージを指定します。このパッケージ以下のクラスの@Componentや@Controllerが付いたクラスが自動的にコンポーネントとして登録されます。
もう一つの設定はViewの設定で、デフォルトとしてJSPは/WEB-INF/jsp/ディレクトリ以下に.jspという拡張子として処理します。Viewのresolverについてはどこかできっと解説します。
次は、web.xmlで指定されていたcommon.jspの設定です。JSTLやSpringのタグライブラリーを指定しておきます。必要に応じて設定してください。
<%@page language="java" pageEncoding="utf-8" %><%-- --%><%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%-- --%><%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %><%-- --%><%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %><%-- --%><%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>
実際に表示するJSPです。
<%@page contentType="text/html; charset=utf-8" %><%-- --%><!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>サンプル</title> </head> <body> Hello world<br> こんにちは世界 </body> </html>
最後にControllerクラスです。
com.example.spring.controller.HelloController
package com.example.spring.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class HelloController { @RequestMapping(value = "/", method = RequestMethod.GET) public String index() { return "hello/index"; } }
Controllerクラスには必ず@Controllerアノテーションを付けます。実際にリクエストを受け付けるメソッドには@RequestMappingアノテーションを付けます。この例では/へのGETメソッドのリクエストを受けつけ、/WEB-INF/jsp/hello/index.jspのJSPへフォワードします。
サーバーを起動して、/にアクセスするとHello worldが表示されると思います。
ソースは https://github.com/kuwalab/spring-mvc40 にあります。タグ001が今回のサンプルです。
まとめ http://kuwalab.hatenablog.jp/entry/spring
最初 http://kuwalab.hatenablog.jp/entry/spring_mvc/001
前回
次回 http://kuwalab.hatenablog.jp/entry/spring_mvc/002