ダウンロード – リンク://wp-content/uploads/2010/07/Struts2-I18n-Tag-Example.zip[Struts2-I18n-Tag-Example.zip]
Struts 2 ”
i18n
“タグは、現在のアクションに関連付けられているリソースバンドルだけでなく、宣言されたリソースバンドルからメッセージを取得するために使用されます。以下の完全な ”
i18n
“タグの例を参照してください:
1.アクション
リクエストを転送するアクションクラス。
package com.mkyong.common.action; import com.opensymphony.xwork2.ActionSupport; public class I18nTagAction extends ActionSupport{ public String execute() throws Exception { return SUCCESS; } }
2.プロパティファイル
デモ用の2つのプロパティファイル。
-
I18nTagAction.properties **
i18n.msg = "This is a message from I18nTagAction.properties"
-
Custom.properties **
i18n.msg = "This is a message from Custom.properties"
3. i18nタグの例
それは ”
i18n
“タグの使用を示しています。
-
i18n.jsp **
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <h1>Struts 2 i18n tag example</h1> <h2>1.Get message from I18nTagAction.properties</h2> Output : <s:text name="i18n.msg"/> <h2>2.Get message from Custom.properties</h2> Output : <s:i18n name="com/mkyong/common/action/Custom"> <s:text name="i18n.msg"/> </s:i18n> </body> </html>
-
それがどのように働きますか?**
{空} 1。例1では、現在のアクション・クラス(
I18nTagAction.java
)に関連付けられているリソース・バンドル(
I18nTagAction.properties
)からメッセージを取得します。
{空} 2。例2では、 com/mkyong/common/action/
フォルダにある ”
Custom.properties ** “プロパティファイルからメッセージを取得します。
-
.properties suffixを出力しない
i18n
タグでよくある間違いで、.properties接尾辞付きのプロパティー・ファイルを宣言した場合、Struts 2は宣言されたリソース・バンドルからメッセージを取得できません。
間違った道** :
<s:i18n name="com/mkyong/common/action/Custom.properties"> <s:text name="i18n.msg"/> </s:i18n>
-
CORRECT WAY
:.propertiesサフィックスなしでプロパティファイル
を宣言しました。
<s:i18n name="com/mkyong/common/action/Custom"> <s:text name="i18n.msg"/> </s:i18n>
4. struts.xml
リンク〜
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true"/> <package name="default" namespace="/" extends="struts-default"> <action name="i18nTagAction" class="com.mkyong.common.action.I18nTagAction" > <result name="success">pages/i18n.jsp</result> </action> </package> </struts>