Struts 2テキストタグの例

Struts 2テキストタグの例

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

Struts 2の「text」タグは、アクションクラスにバンドルされているリソースバンドルからメッセージを取得するために使用されます。 そして、3つのシーケンスに従ってください:

  1. リソースバンドルからのメッセージを表示し、Struts 2 resource bundle search orderに従います。

  2. リソースバンドルでメッセージが見つからない場合、タグの本文が表示されます。

  3. タグの本文が空の場合、タグの「name」属性の値が表示されます。

これは完全な例で最もよく説明されています:

1. アクション

要求を転送するActionクラス。

TextTagAction.java

package com.example.common.action;

import com.opensymphony.xwork2.ActionSupport;

public class TextTagAction extends ActionSupport{

    public String execute() throws Exception {

        return SUCCESS;
    }
}

2. プロパティファイル

2つのキー「name.msg」と「name.msg.param」を持つ単純なプロパティファイル。

TextTagAction.properies

name.msg = "This is a message from properties file"
name.msg.param = "This is a message from properties file - param : {0}"

3. テキストタグの例

「テキスト」タグの使用方法を示しています。

text.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>





Struts 2 text tag example

1.

Output :

2. message doesn't exists

Output : message doesn't exists

3.

Output :

4. example

Output : example

How it work?
1. <s:text name=”name.msg” />
現在のアクションクラス(TextTagAction.action)に関連付けられているリソースバンドル(TextTagAction.properies)からメッセージを取得して表示します。

 "This is a message from properties file"

2. <s:text name=”name.msg.unknow”>message doesn’t exists</s:text>
キーがリソースバンドル「TextTagAction.properies」または検索順序に見つからないため、タグの本文が表示されます。

message doesn't exists

3. <s:text name=”name.msg.unknow” />
リソースバンドルとタグの本文にメッセージが見つからないため、「name」属性の値が表示されます。

name.msg.unknow

4. <s:text name=”name.msg.param” ><s:param >example</s:param></s:text>
タグを介してパラメーターをリソースバンドルに渡します。

"This is a message from properties file - param : example"

4. struts.xml

リンクしてください〜




    
    

        
            pages/text.jsp