datetagaction.java
package com.h3.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.h3.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.h3.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