{"id":6818,"date":"2013-01-25T23:03:28","date_gmt":"2013-01-25T14:03:28","guid":{"rendered":"http:\/\/arison.jp\/blog\/?p=6818"},"modified":"2017-02-25T22:24:51","modified_gmt":"2017-02-25T13:24:51","slug":"how-to-get-the-value-from-the-properties-file-in-struts2","status":"publish","type":"post","link":"https:\/\/arison.jp\/blog\/2013\/01\/25\/how-to-get-the-value-from-the-properties-file-in-struts2\/","title":{"rendered":"\u3010Java\u3011Struts2\u3067\u306e\u30d7\u30ed\u30d1\u30c6\u30a3\u30d5\u30a1\u30a4\u30eb\u304b\u3089\u5024\u3092\u53d6\u5f97\u3059\u308b\u65b9\u6cd5"},"content":{"rendered":"<p><span class=\"font24px\">Struts2\u3067\u306e\u30d7\u30ed\u30d1\u30c6\u30a3\u30d5\u30a1\u30a4\u30eb\u304b\u3089\u5024\u3092\u53d6\u5f97\u3059\u308b\u65b9\u6cd5<\/span><\/p>\n<p>\u3044\u307e\u3055\u3089\u3001Struts2\u3068\u3044\u308f\u308c\u305f\u3089\u3002\u305d\u308c\u307e\u3067\u306a\u3093\u3060\u3051\u3069\u3002<br \/>\nStruts2\u3067\u30d7\u30ed\u30d1\u30c6\u30a3\u30d5\u30a1\u30a4\u30eb\u304b\u3089\u5024\u3092\u53d6\u5f97\u3059\u308b\u65b9\u6cd5\u304c\u3042\u3063\u3066\u9806\u756a\u306f\u4e0b\u8a18\u306e\u901a\u308a\u3002<\/p>\n<p><!--more--><\/p>\n<p><a href=\"http:\/\/struts.apache.org\/2.x\/docs\/localization.html\" title=\"Localization\">Localization<\/a><\/p>\n<hr \/>\n<p>Resource Bundle Search Order<br \/>\nResource bundles are searched in the following order:<\/p>\n<p>1.ActionClass.properties<br \/>\n2.Interface.properties (every interface and sub-interface)<br \/>\n3.BaseClass.properties (all the way to Object.properties)<br \/>\n4.ModelDriven&#8217;s model (if implements ModelDriven), for the model object repeat from 1<br \/>\n5.package.properties (of the directory where class is located and every parent directory all the way to the root directory)<br \/>\n6.search up the i18n message key hierarchy itself<br \/>\n7.global resource properties <\/p>\n<hr \/>\n<p><span class=\"font20px\">\u30d7\u30ed\u30d1\u30c6\u30a3\u30d5\u30a1\u30a4\u30eb\u3092\u4e00\u5143\u7ba1\u7406\u3057\u305f\u3044\u306a\u3089\u30017.global resource properties \u3067\u3044\u3044\u3093\u3058\u3083\u306a\u3044\u304b\u306a\u3068\u3002<\/span><\/p>\n<p>[java]<br \/>\nActionSupport as = new ActionSupport();<br \/>\nas.getText(&quot;hoge&quot;)<br \/>\n[\/java]<\/p>\n<p>\u3053\u306e\u6642\u306b\u53d6\u5f97\u3067\u304d\u308b\u306e\u304c7\u3060\u3051\u3060\u3063\u305f\u3002<br \/>\n1\uff5e6\uff08\u7279\u306b5.package.propertie\u3067\u753b\u9762\u8868\u793a\u7528\u306b\u4e00\u5143\u7ba1\u7406\u3057\u3066\u3044\u305f\u5834\u5408\uff09\u306f\u30017\u306b\u79fb\u52d5\u3055\u305b\u305f\u65b9\u304c\u3044\u3044\u3068\u601d\u3046\u3002<br \/>\n\u3068\u3044\u3046\u306e\u306f\u2026<\/p>\n<p>\u3010\u5c0f\u8a00\u958b\u59cb\u3011<br \/>\nJsp\u8868\u793a\u6642\u306b\u30ea\u30b9\u30c8\u30dc\u30c3\u30af\u30b9\u3092\u51fa\u529b\u3059\u308b\u3068\u304d\u306bLinkedHashMap(\u30ad\u30fc\u3001\u5024)\u3067\u4f5c\u6210\u3059\u308b\u3068\u8868\u793a\u3057\u3084\u3059\u3044\u3002\uff08\u4e0b\u8a18\u4f8b\uff09<\/p>\n<hr \/>\n<p>jsp\u5074\u3067\u306e\u53d6\u5f97\u65b9\u6cd5<br \/>\n[xml]<br \/>\n&lt;s:select name=&quot;hogeName&quot; list=&quot;getHogeMap()&quot;<br \/>\n emptyOption=&quot;true&quot; cssStyle=&quot;width:20em;&quot;&gt;&lt;\/s:select&gt;<br \/>\n[\/xml]<\/p>\n<hr \/>\n<p>java\u5074\u3067\u306e\u4f5c\u6210\u65b9\u6cd5<br \/>\n[java]<br \/>\npublic Map&lt;string , String&gt; getHogeMap() {<br \/>\n  Map&lt;\/string&gt;&lt;string , String&gt; hogeMap = new LinkedHashMap&lt;\/string&gt;&lt;string , String&gt;();<br \/>\n  hogeMap.put(hogeKey1, getText(&quot;hoge1&quot;));<br \/>\n  hogeMap.put(hogeKey2, getText(&quot;hoge2&quot;));<br \/>\n  hogeMap.put(hogeKey3, getText(&quot;hoge3&quot;));<br \/>\n  return hogeMap<br \/>\n}<br \/>\n[\/java]<\/p>\n<hr \/>\n<p>\u5024\u3092\u56fd\u969b\u5316\u5bfe\u5fdc\u3059\u308b\u305f\u3081getText\u304c\u5fc5\u8981\u3060\u3063\u305f\u306e\u3067\u3001action\u30af\u30e9\u30b9\u3067\u4f5c\u6210\u3057\u3066\u3044\u305f\u3002<br \/>\n\u3067\u3001\u540c\u3058\u30ea\u30b9\u30c8\u30dc\u30c3\u30af\u30b9\u306a\u306e\u306b\u6bce\u56de\u4f5c\u6210\u3057\u3066\u3044\u3066\u3001\u5171\u901a\u51e6\u7406\u3067\u304d\u306a\u3044\u304b\u306a\u3068\u601d\u3063\u305f\u306e\u304c\u304d\u3063\u304b\u3051\u3002<\/p>\n<p><span class=\"font20px\">\u4ed6\u306b\u3082\u3001\u30ed\u30b0\u51fa\u529b\u306e\u56fd\u969b\u5316\u3001\u30d7\u30ed\u30d1\u30c6\u30a3\u53d6\u5f97\u3068\u304b\u3092\u8003\u3048\u305f\u3089\u30017.global resource properties \u3067\u3044\u3044\u3093\u3058\u3083\u306a\u3044\u304b\u306a\u3068\u3002<\/span><\/p>\n<p>getText\u306fAction\u30af\u30e9\u30b9\u3060\u3051\u3067\u4f7f\u7528\u3059\u3079\u304d\u8ad6\u3001<br \/>\nActionSupport\u3092\u7d99\u627f\u3057\u305f\uff5eAction\u3067\u306f\u306a\u3044\u30af\u30e9\u30b9\u3092\u4f5c\u6210\u3057\u3066\u3001\u305d\u3053\u304b\u3089getText\u3059\u308b\u8ad6<br \/>\nabstract\u306eAction\u30af\u30e9\u30b9\u4f5c\u3063\u3066\u5171\u901a\u51e6\u7406\u3059\u308b\u8ad6<br \/>\n\u306a\u3069\u3044\u308d\u3044\u308d\u65b9\u6cd5\u3084\u8003\u3048\u65b9\u306f\u51fa\u305f\u3093\u3060\u3051\u3069\u2026\u3002<\/p>\n<p><span class=\"font20px\">7.global resource properties \u3067\u3044\u3044\u3093\u3058\u3083\u306a\u3044\u304b\u306a\u3068\u3002<\/span><\/p>\n<p>\u3010\u5c0f\u8a00\u7d42\u4e86\u3011<\/p>\n<p>7.global resource properties\u3092\u4f7f\u3046\u305f\u3081\u306b\u306f\u3001struts.xml\u306b\u3066\u4f7f\u3046\u30d7\u30ed\u30d1\u30c6\u30a3\u30d5\u30a1\u30a4\u30eb\u3092\u5b9a\u7fa9\u3059\u308c\u3070\u4f7f\u3048\u308b\u3002<\/p>\n<p>struts.xml(\u4e00\u90e8\u629c\u7c8b)<br \/>\n[xml]<br \/>\n&lt;struts&gt;<br \/>\n    &lt;!&#8211; \u30d7\u30ed\u30d1\u30c6\u30a3\u30d5\u30a1\u30a4\u30eb &#8211;&gt;<br \/>\n    &lt;constant name=&quot;struts.custom.i18n.resources&quot; value=&quot;global&quot;&gt;&lt;\/constant&gt;<br \/>\n[\/xml]<\/p>\n<hr \/>\n<p>global\u306f\u81ea\u7531\u306b\u5909\u66f4\u53ef\u80fd<\/p>\n<p>i18n\u3092\u4f7f\u3046\u306e\u3067\u3001\u540d\u524d\u306e\u6700\u5f8c\u306b&#8221;_ja&#8221;\u3068\u3064\u3051\u308b\u4e8b\u3067\u56fd\u969b\u5316\u5bfe\u5fdc\u5b8c\u4e86\u3002<br \/>\n_ja\u306b\u65e5\u672c\u8a9e\u3001_en\u306b\u82f1\u8a9e\u3067\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u3092\u8a18\u8ff0\u3059\u308c\u3070\uff2f\uff2b\u3002<br \/>\n\u307e\u3042\u3001\u82f1\u8a9e\u3092\u30c7\u30d5\u30a9\u30eb\u30c8\u306b\u3059\u308b\u306a\u3089\u3001global_ja.properties\uff08\u65e5\u672c\u8a9e\uff09\u3068global.properties\uff08\u82f1\u8a9e\uff09\u3067\uff2f\uff2b\u3002<br \/>\n\u305d\u3046\u3059\u308c\u3070\u3001\u5730\u57df\u306e\u30ed\u30b1\u30fc\u30b7\u30e7\u30f3\u306b\u3088\u308ai18n\u3067\u4f7f\u3046\u30d5\u30a1\u30a4\u30eb\u3092\u9078\u3093\u3067\u304f\u308c\u308b\u3068\u3002<\/p>\n<p>\u914d\u7f6e\u5834\u6240\u306fWEB-INF\/classes\u306b\u3001global_ja.properties \u3068 global.properties\u3002<\/p>\n<hr \/>\n<p>java\u5074\u3067\u306e\u53d6\u5f97\u65b9\u6cd5<\/p>\n<p>Action\u30af\u30e9\u30b9\u3067\u306f\u5143\u3005\u7d99\u627f\u3057\u3066\u3044\u308b\u306e\u3067\u5ba3\u8a00\u3059\u308b\u5fc5\u8981\u306a\u3057<\/p>\n<p>validator\u306e\u5834\u5408\u3001<br \/>\n@RequiredStringValidator(key=&#8221;sample.test&#8221;)<\/p>\n<p>\u4ed6\u306e\u90e8\u5206\u3067\u53d6\u5f97\u3057\u305f\u3044\u5834\u5408\u306f\u4e0b\u8a18\u3002<br \/>\n[java]<br \/>\nActionSupport as = new ActionSupport();<br \/>\nas.getText(&quot;hoge&quot;)<br \/>\n[\/java]<\/p>\n<hr \/>\n<p>jsp\u5074\u3067\u306e\u53d6\u5f97\u65b9\u6cd5<br \/>\n[xml]<br \/>\n&lt;s:text name=&quot;hoge&quot;&gt;&lt;\/s:text&gt;<br \/>\n&lt;s:submit method=&quot;hoge&quot; value=&quot;%{getText(&#8216;hoge&#8217;)}&quot;&gt;&lt;\/s:submit&gt;<br \/>\n[\/xml]<\/p>\n<hr \/>\n<p>\u3010\u53c2\u8003\u30b5\u30a4\u30c8\u3011<br \/>\nstruts2properties\uff08\u30d7\u30ed\u30d1\u30c6\u30a3\u30d5\u30a1\u30a4\u30eb\uff09\u306e\u8a2d\u5b9a\u3068\u53d6\u5f97\uff5c\u30d7\u30ed\u30b0\u30e9\u30e0\u5782\u308c\u6d41\u3057<br \/>\nhttp:\/\/ameblo.jp\/tyoku123\/entry-10439370680.html<\/p>\n<p>CMS Labs Struts 2\u306e\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u30a8\u30e9\u30fc\u30fb\u30e1\u30c3\u30bb\u30fc\u30b8&#038;\u56fd\u969b\u5316\u3078\u306e\u5bfe\u5fdc<br \/>\nhttp:\/\/cmslabs.blogspot.jp\/2011\/02\/struts-2_21.html<\/p>\n<p>Struts2\u30ed\u30fc\u30ab\u30e9\u30a4\u30ba\u30b5\u30f3\u30d7\u30eb<br \/>\nhttp:\/\/www.kobu.com\/i18n\/struts2\/index.htm<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Struts2\u3067\u306e\u30d7\u30ed\u30d1\u30c6\u30a3\u30d5\u30a1\u30a4\u30eb\u304b\u3089\u5024\u3092\u53d6\u5f97\u3059\u308b\u65b9\u6cd5 \u3044\u307e\u3055\u3089\u3001Struts2\u3068\u3044\u308f\u308c\u305f\u3089\u3002\u305d\u308c\u307e\u3067\u306a\u3093\u3060\u3051\u3069\u3002 Struts2\u3067\u30d7\u30ed\u30d1\u30c6\u30a3\u30d5\u30a1\u30a4\u30eb\u304b\u3089\u5024\u3092\u53d6\u5f97\u3059\u308b\u65b9\u6cd5\u304c\u3042\u3063\u3066\u9806\u756a\u306f\u4e0b\u8a18\u306e\u901a\u308a\u3002<\/p>\n","protected":false},"author":2,"featured_media":17178,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,4],"tags":[703,120],"class_list":["post-6818","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming","category-diary","tag-java","tag-struts2"],"_links":{"self":[{"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/posts\/6818","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/comments?post=6818"}],"version-history":[{"count":0,"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/posts\/6818\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/media\/17178"}],"wp:attachment":[{"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/media?parent=6818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/categories?post=6818"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/tags?post=6818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}