SpringでWEB作成するには?(基本的な作成例) | Java Springの逆引きメモ

Java Springの逆引きメモ

JavaのSpring frameworkのメモを書いていきます!
初心者の勉強ノートなので間違いがあるかもしれませんが、何かヒントになることがあれば幸いです。

前回は、SpringのWEBプロジェクトを作成するまでを見てみました。

ここでは実際にWEBを表示するまでの、基本的なSpringの設定を見てみましょう。

プロジェクトとSpringのXMLファイルを作成していない人は、前回の記事 を読んで設定してください。



【簡単なWEBを作ってみる】

さて、実際に表示できるWEBを作ってみましょう。

やることは、JSPファイルを作成することと、Springの設定ファイルを作成することです。



<web.xml>


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee "
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance "
xsi:schemaLocation="
http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ">


<display-name>test</display-name>


<!-- spring用のリスナー(これ書かないとapplicationContext.xmlを読まない) -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


<!-- エンコード(パラメタの文字コードを指定します) -->
<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>

<!-- springMVC用の設定(MVCにstrutsを利用する場合は不要) -->
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>


<!--

 以下は、パスの拡張子がhtmlのときにSpringを適用するように記述しています。

-->
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>

<session-config>
<session-timeout>60</session-timeout>
</session-config>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>


<!--
 読み込むspring設定ファイルの指定
 xxx-servlet.xmlはデフォルトで読み込むのでここで設定する必要がありません。
-->

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>


</web-app>



<test-servlet.xmlファイル>

次に、SpringのWEBの中心設定をするファイルです。

前回の記事の設定で作成したファイルに以下の記述をします。



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


<!--
 メッセージリソースの設定。
 bean名はmessageSourceにする必要があります。
-->

<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages"/>
</bean>


<!-- ExceptionResolver (例外発生時にどのページを表示するか?)
bean名はexceptionResolverにする必要があります。

コントローラやビジネスロジックでは,この方法で例外処理を行えます。
ただし、ビューやフィルタで例外が発生した場合は処理されません。
web.xmlでerror-pageタグを利用して,例外処理を行う必要があります。
http://itpro.nikkeibp.co.jp/article/COLUMN/20080523/303910/?P=2
-->
<bean id="exceptionResolver"
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="java.lang.Exception">/WEB-INF/jsp/error.jsp</prop>
</props>
</property>
</bean>


<!-- HandlerMapping (パス名とControllerを結び付けます)-->
<bean id="handlerMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/top.html">fileNameController</prop>
<prop key="/next.html">fileNameController</prop>
</props>
</property>
</bean>



<!--

 サンプルController
 コントローラは、xxx-servlet.xmlファイル内に記述しないと、messageのLocaleが設定されない。
-->

<bean id="fileNameController" class="org.springframework.web.servlet.mvc.UrlFilenameViewController">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />

</bean>

</beans>



<applicationContext.xmlファイル>

このファイルは、今回は特に変更しません。




<jspファイル: WebContent/WEB-INF/index.jsp>

 index.jspというファイル名で作成します。

 Springで管理しているページに遷移するリンクを作るだけの特に意味の無いページです。


<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd ">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>単純なindex画面です</title>
</head>
<body>
index画面<br>
<br>
<a href="top.html">Springの画面へ</a>

</body>
</html>




<Spring用のjspファイル: WebContent/WEB-INF/jsp/top.jsp>

 jspフォルダの下にtop.jspというファイル名で作成します。

 上記のSpringのhandlerMapping が働いているかを見るためのJSPです。


<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd ">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>単純な画面です</title>
</head>
<body>
単純トップ画面<br>
<br>
<a href="next.html">次の画面</a>

</body>
</html>



<Spring用のjspファイル: WebContent/WEB-INF/jsp/next.jsp>

 jspフォルダの下にnext.jspというファイル名で作成します。 


<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd ">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>単純な次の画面です</title>
</head>
<body>
画面遷移を見るためのサンプル<br>
<br>
<a href="top.html">前の画面に戻る</a>

</body>
</html>





  
【終了です!】

プロジェクトを右クリックして、実行⇒サーバーで実行 を選択しましょう。

どうです?うまく動きましたか?



【Spring設定ファイルの説明】

test-servlet.xmlについて、軽く触れます。


ここでは、以下のような設定をしています。


fileNameController
 単純にJSPファイルを表示するだけのコントローラーです。

 表示するJSPファイルの実パスは以下のように推測します。

   prefix + URLパス名(拡張子なし) + suffix

 上記のXMLファイルのfileNameControllerの設定を見てみてください。

 例えば、http://localhost:8080/test/top.html

 というパスに対しては、/WEB-INF/jsp/top.jsp というJSPファイルを検索して表示します。

 パスと拡張子を自由に変更できるので、便利です。


handlerMapping
 次に見るのはURLパスの設定です。

 このクラスは、あるURLにアクセスに来たときに、そのコントローラを起動するかを決めます。

 上記のXMLファイルのhandlerMappingの設定を見てみてください。

 例えば以下の設定の場合、

  <prop key="/top.html">fileNameController</prop>


 /top.html というアクセスに対しては、fileNameControllerを起動することになります。
 このコントローラは、先ほど見たように、JSPファイルのパスを推測して表示します。

 つまり、/WEB-INF/jsp/top.jsp を表示することになります。



messageSourceexceptionResolverは、ここでは使用していませんが、

遅かれ早かれ使用することになると思います。

ですので、紹介がてら記述しておきました。

興味があれば調べてみてください。


なんとなくやっていることがわかりましたでしょうかえっ
割と簡単でしょ?



ここでは、Springで用意されているコントローラを使用したので、自作のコントローラを作成しませんでしたが、

他の記事でそのあたりは見て行こうと思います。



参考:

・Springを使用するには?

・DIの設定ファイルを書くには?

・Springとは? (機能一覧と概要)

・Springにおけるファイル構成
・XMLとは?