トップページ >  Struts >  メッセージリソース
初版2006/08/10 最終更新日2006/08/10
  メッセージリソース
目次
メッセージリソースとは
メッセージリソースの読み込み
メッセージリソースの例
リソースファイル内に日本語を記述
リソースファイル内のコメント
リソースファイル内のコメント
メッセージリソースとは
メッセージリソースとは、エラーメッセージやボタンの名前などを設定するためのファイルです。

メッセージリソースの読み込み
メッセージリソースを読み込むにはstruts-config.xmlファイル内にメッセージリソースの読み込み設定を記述する必要があります。
以下は、WEB-INF\classes配下にあるMessageResources.propertiesファイルを読み込む例です。

<message-resources parameter="MessageResources" />

リソースファイルの位置はデフォルトではWEB-INF/classes/配下になります。
ですので、上記のようにファイル名のみを設定する場合はWEB-INF/classes配下に置きます。
実際のファイル名は.propertiesがつきますが、struts-config内では.propertiesは記述しません。

メッセージリソースの例
メッセージリソースは具体的には以下のような内容になります。
キーとメッセージを=で結ぶだけです。

#   Licensed to the Apache Software Foundation (ASF) under one or more
#   contributor license agreements.  See the NOTICE file distributed with
#   this work for additional information regarding copyright ownership.
#   The ASF licenses this file to You under the Apache License, Version 2.0
#   (the "License"); you may not use this file except in compliance with
#   the License.  You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

# -- standard errors --
errors.header=<UL>
errors.prefix=<LI>
errors.suffix=</LI>
errors.footer=</UL>
# -- validator --
errors.invalid={0} is invalid.
errors.maxlength={0} can not be greater than {1} characters.
errors.minlength={0} can not be less than {1} characters.
errors.range={0} is not in the range {1} through {2}.
errors.required={0} is required.
errors.byte={0} must be an byte.
errors.date={0} is not a date.
    ・
    ・
    ・

リソースファイル内に日本語を記述
リソースファイル内はデフォルトでは日本語記述は対応していません。
置換文字もアプリ側で日本語にすると、正しく表示されません。

日本語対応にするにはUnicode変換する必要があります。
これはnative2asciiツールなどで簡単に変換できます。これで置換文字も含め日本語に対応できるようになります。
native2asciiについては こちらを御覧ください。

リソースファイル内のコメント
リソースファイル内では # 以降はその行はコメントになります。
以下コメント例です。

#ここはコメント
error={0}は必須です。#ここもコメント

行の、#以降がコメントになるので、#以前はコメントになりません。

リソースファイル内のコメント
メッセージの改行は \ で行います。