Struts 2 i18nタグの例

Struts 2 i18nタグの例

ダウンロード–Struts2-I18n-Tag-Example.zip

Struts 2の「i18n」タグは、現在のアクションに関連付けられているリソースバンドルだけでなく、宣言されているリソースバンドルからメッセージを取得するために使用されます。 以下の完全な「i18n」タグの例を参照してください。

1. アクション

要求を転送するアクションクラス。

I18nTagAction.java

package com.example.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" %>





Struts 2 i18n tag example

1.Get message from I18nTagAction.properties

Output :

2.Get message from Custom.properties

Output :

それがどのように働きますか?

1. 例1では、現在のアクションクラス(I18nTagAction.java)に関連付けられているリソースバンドル(I18nTagAction.properties)からメッセージを取得します。

2. 例2では、​​com/example/common/action/フォルダーにある「Custom.properties」プロパティファイルからメッセージを取得します。

DO NOT PUT .properties suffix
i18nタグでよくある間違いです。プロパティファイルに.propertiesサフィックスを付けて宣言した場合、Struts 2は宣言されたリソースバンドルからメッセージを取得できませんでした。
WRONG WAY


    

CORRECT WAY
プロパティファイルwithout a .properties suffixを宣言しました。


    

4. struts.xml

リンクしてください〜




    
    

        
            pages/i18n.jsp