トッカンソフトウェア

Spring MVCの設定ファイル(web.xml)

前回はとりあえず動作させたので、今回から説明していきます。
今回は設定ファイル(web.xml)をやります。


Spring設定ファイル(Bean定義ファイル)の指定

				

<?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"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	id="WebApp_ID" version="3.1">
	<display-name>springMVC</display-name>

	<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>
	<servlet>
		<servlet-name>dispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value></param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>dispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

</web-app>


			
最初に org.springframework.web.context.ContextLoaderListener クラスをリスナー登録しています。
これが登録されていることにより、Spring設定ファイル(/WEB-INF/applicationContext.xml)を読みに行きます。
このパスやファイル名称はcontextConfigLocationで変更することが出来ます。

■/WEB-INF/spring/app.xmlに変更
				
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring/app.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
		
	
			

Spring設定ファイルはサーブレット毎に設定出来ます。設定する場合は init-param 内の contextConfigLocation で設定します。

■サーブレットに/WEB-INF/spring/mvc-servlet.xmlに適用
				

	<servlet>
		<servlet-name>dispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring/mvc-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>


			

org.springframework.web.servlet.DispatcherServlet について

普通のWEBアプリケーションではServletを作成しますが、Spring Web MVCでは作成せず、Springが用意してくれている
org.springframework.web.servlet.DispatcherServletを使用します。

DispatcherServletがクライアントからの要求を受け付けて、コントローラクラス(@Controller)に割り振って、コントローラクラス
からの戻りからビュー(jsp)を割り振ってクライアントに戻して・・・などSpring MVCの全体コントロールを行いますが、ソースや
設定ファイル上で登場するのは、このweb.xmlだけです。

なので、Spring MVC でWEBアプリを開発時は、ほとんど意識することはありませんが、裏で色々と動いてくれています。

CharacterEncodingFilter

クライアントから送られたデータを指定した文字コードに強制的にエンコーディングします。
これはHttpServletRequestのsetCharacterEncodingを裏で実行してくれます。

				

	<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>


			


ページのトップへ戻る