DateTagAction.java
package com.tw511.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; } }
date.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head><title>Struts2 <s:date>標籤範例 - www.tw511.com</title> </head>
<body>
<h1>Struts2 <s:date>標籤範例</h1>
<ol>
<li>
Default date format
--> <strong><s:date name="customDate" /></strong>
</li>
<li>
Date format in "yyyy-MM-dd"
--> <strong><s:date name="customDate" format="yyy-MM-dd" /></strong>
</li>
<li>
In Date tag, set the nice attribute to "true"
--> <strong><s:date name="customDate" nice="true" /></strong>
</li>
</ol>
</body>
</html>
<?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="dateTagAction" class="com.tw511.common.action.DateTagAction" > <result name="success">/pages/date.jsp</result> </action> </package> </struts>
http://localhost:8080/struts2datetag/dateTagAction.action
在瀏覽器中開啟上面的網址,顯示結果如下:
程式碼下載 - http://pan.baidu.com/s/1dDe7YLz