ダウンロードする – リンク://wp-content/uploads/2010/07/Struts2-Date-Tag-Example.zip[Struts2-Date-Tag-Example.zip]

Struts 2 ”

date

“タグは、Dateオブジェクトを次の2つの方法で整形するために使用されます。

  1. カスタム日付形式(例: “dd/MM/yyyy”).

  2. のような読みやすい表記法に日付をフォーマットする “素敵な”属性

“この日は162日前です”。

このチュートリアルでは、Struts 2 ”

date

“タグを使用してDateオブジェクトを ”

カスタム日付形式

“と ”

easy readable notations

“にフォーマットしています。

1.アクション

要求を転送し、あらかじめ定義された日付のDateオブジェクトを初期化するためのActionクラス。

package com.mkyong.common.action;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;

public class DateTagAction extends ActionSupport{

    public Date customDate;

    public String execute() {

        Calendar cal = Calendar.getInstance();
       //set date to january 31, 2010
        cal.set(2010, 0, 31);
        Date newDate = cal.getTime();

        setCustomDate(newDate);

        return SUCCESS;

    }

    public Date getCustomDate() {
        return customDate;
    }

    public void setCustomDate(Date customDate) {
        this.customDate = customDate;
    }

}

2.日付タグの例

Dateオブジェクトの書式設定に「

date

」タグの使用を示すJSPページ:

  1. デフォルトの日付形式.

  2. カスタム日付形式.

  3. 読みやすい表記法.

    • date.jsp **

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

<body>
<h1>Struts 2 date tag example</h1>

<ol>

<li>
Default date format
--> <strong><s:date name="customDate"/></strong>

</li>

<li>
日付形式は "dd/MM/yyyy"
 - > <strong> <s:date name = "customDate" format = "dd/MM/yyyy"/> </strong>
</li>

<li>
Dateタグで、nice属性を "true"に設定します。
 - > <strong> <s:date name = "customDate" nice = "true"/> </strong>

</li>

</ol>

</body>
</html>

3. struts.xml

リンク〜

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE struts PUBLIC
" - //Apache Software Foundation//DTD Strutsコンフィグレーション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">
    
        <アクション名= "dateTagAction"
            class = "com.mkyong.common.action.DateTagAction">
            <result name = "success"> pages/date.jsp </result>
        </action>
        
    </package>
</struts>