SpringMVC3.2.1を利用してのWebアプリケーション #02
前回の続きです。前回はQuickstartで作成したプロジェクトをそのまま動作させましたが、今回はライブラリのバージョン等を変更していきます。
pom.xmlの編集
pom.xmlを記載すると冗長になるので主な変更点だけ。
- springframework-version を 3.2.1.RELEASEに変更
- dependency に spring-web を追加
- spring-web はこれまで spring-webmvc に依存していたため自動的に追加されていたが、依存関係が変更になったため追加が必要
- dependency から logback-classic を削除
- dependency に slf4j-log4j12 と log4j を追加
- log4j のバージョンは1.2.15。exclusions に mail 、jms 、jmxtools 、jmxri を指定
- dependency から geronimo-servlet_3.0_spec を削除
- dependency に javax.servlet-api を追加
- バージョンは3.0.1。
- dependency の jsp-api のバージョンを変更
- バージョンは 2.2。
- dependency の junit のバージョンを変更
- バージョンは 4.11。
ロガーをlog4jに変更しました。その他はバージョンの修正がメインです。
各種定義ファイルの作成
Quickstartの場合、これまでならばweb.xmlに記載していたような設定情報をJavaで設定しています。これはSpring3.1から追加されたWebApplicationInitializer を利用することで可能となります。
最終的にはそれでも良いんですが、今の段階だとどの情報がどのJavaクラスと紐付くのかイマイチ把握できないため、まずはこれまで通りにweb.xmlやApplicationContext.xmlに定義情報を記述する方式へ戻していきます。
web.xml
まずは web.xml から。パスは springmvc-tutorial/src/main/webapp/WEB-INF/web.xml です。
<?xml version="1.0" encoding="UTF-8"?> <web-app 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" version="3.0"> <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> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/ApplicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> </web-app>
Quickstartではspring-securityの設定も行っていますが、ここでは一旦除いておき、実際に利用するようになったら追加します。
ApplicationContext.xml
続いて ApplicationContext.xml 。パスは springmvc-tutorial/src/main/webapp/WEB-INF/spring/ApplicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config /> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> </bean> <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basenames"> <list> <value>classpath:message/message</value> <value>classpath:message/label</value> </list> </property> </bean> <context:component-scan base-package="jp.co.shantery.tutorial.service" /> <context:component-scan base-package="jp.co.shantery.tutorial.dao" /> </beans>
servlet-context.xml
最後に servlet-context.xml です。パスは web.xmlに記載した通り、 springmvc-tutorial/src/main/webapp/WEB-INF/spring/servlet-context.xml です。
SpringMVCの設定を記載します。
<?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" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <mvc:annotation-driven /> <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/view/" /> <property name="suffix" value=".jsp" /> </bean> <context:component-scan base-package="jp.co.shantery.tutorial.web.*.controller" /> </beans>
コントローラとJSPの作成
簡単なコントローラとJSPを作成して、いつものHello World画面を表示してみます。まずはコントローラから。パッケージは「jp.co.shantery.tutorial.web.hello.controller」としました。
package jp.co.shantery.tutorial.web.hello.controller; import java.util.Date; import org.apache.log4j.Logger; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; /** * HelloWorldを表示するコントローラです。 * * @author m-namiki * */ @Controller public class HelloWorldController { private static Logger logger = Logger.getLogger(HelloWorldController.class); /** * hello.jspを表示します。 * * @return hello.jspのパス */ @RequestMapping("/hello") public ModelAndView index() { logger.info("index() Start."); ModelAndView view = new ModelAndView(); view.setViewName("hello/hello"); view.addObject("message", "Hello Spring MVC world."); view.addObject("currentDate", new Date()); logger.info("index() End."); return view; } }
次にJSPを作ります。JSPはWEB-INF/view配下に作成する必要があるので、springmvc-tutorial/src/main/webapp/WEB-INF/view/hello/hello.jspに作成します。
<%@ page language="java" trimDirectiveWhitespaces="true" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> <html> <head> <title>Hello World</title> </head> <body> <h1><c:out value="${message}"/></h1> <fmt:formatDate value="${currentDate}" pattern="yyyy-MM-dd HH:mm:ss" /> </body> </html>
後は前回同様に tomcat-maven-plugin から Tomcat7 を起動して、http://localhost:8080/springmvc-tutorial/hello.html にアクセスすると最初にコントローラが呼ばれ、コントローラからJSPが呼ばれてメッセージと日時が表示されます。
今日はここまで。次回はフォームの受け取りやサービスの利用を試してみようと思います。
ついでにこのプロジェクトのGitHubリポジトリはm-namiki/springmvc-tutorial · GitHubです。