トッカンソフトウェア

Spring MVCでMessageSource(WEBアプリケーション)

今回はMessageSourceをやります。MVCでHello Worldをベースに変更部分だけ記述する ので、まずはMVCでHello Worldを作成して下さい。


ソース

applicationContext.xml

				
<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-4.2.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.2.xsd
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">

	<context:component-scan base-package="spring.test" />

	<mvc:annotation-driven />

	<bean id="messageSource"
		class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
		<property name="basename" value="/META-INF/messages" />
	</bean>
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>

			
valueにメッセージファイルの場所、ファイル名を指定します。
上記の例ではMETA-INFフォルダのmessages.properties(messages_ja.propertiesなど)ファイルになります。

TestController.java

				
package spring.test;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {
	@Autowired
	MessageSource message;

	@RequestMapping(value = "/", method = GET)
	public String show(Locale locale) {
		String[] strs = { "あいう", "えお" };

		System.out.println(message.getMessage("msg", null, locale));
		System.out.println(message.getMessage("msg2", strs, locale));
		return "test";
	}
}


			
一番目の引数でメッセージファイルのキーを指定します。
二番目の引数はパラメータを指定し、3番目の引数はロケールを指定します。

ロケールがわからない場合は、Javaが定義している定数を渡して下さい(java.util.Locale.JAPANESEなど)。

test.jsp

				
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html>
<head>
<title>Hello Spring Framework</title>
</head>
<body>
	Hello Spring Framework
	<br />
	<spring:message code="msg" />
	<br />
	<spring:message code="msg2" arguments="あいう,えお" />
</body>
</html>


			

messages_ja.properties

				
msg=メッセージ
msg2=引数あり:{0}:{1}

			

messages.properties

				
msg=message
msg2={0}:{1}:message

			
配置


実行イメージ

ブラウザ


ログ

追加情報①

Javascriptで使用する場合はspring:messageタグをJavascript内に書きます。
javaScriptEscapeを付けるとJavascriptで問題となる文字をエスケープしてくれます。
				
<script type="text/javascript">
	alert("<spring:message code='msg'  javaScriptEscape='true'/>");
</script>

			

追加情報②

メッセージファイルを複数に分ける場合は以下にように指定します。

applicationContext.xmlの一部
				
	<bean id="messageSource"
		class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
		<property name="basenames">
			<list>
				<value>/META-INF/messages</value>
				<value>/META-INF/labels</value>
			</list>
		</property>
	</bean>


			

ページのトップへ戻る