---
title: "Struts 2 日期处理"
author: "fairy_xzc"
date: "2012-09-10T12:47:04Z"
tags: []
categories: ["编程语言"]
slug: "struts-2"
draft: false
lastmod: "2012-09-10T12:48:40Z"
id: 1674965
url: "https://agilestyle.iteye.com/blog/1674965"
---

1.日期格式化

demo1

```
<s:textfield name="contact.birthDate" label="ContactBirthDate">
	<s:param name="value">
		<s:date name="contact.birthDate" format="yyyy-MM-dd" />
	</s:param>
</s:textfield>
```

 

 

demo2

```
<input type="text" value="<s:date name="contact.birthDate" format="yyyy-MM-dd" />" />
```

 

 

2.使用日期控件

加入struts2-dojo-plugin

```
......
	<dependency>
		<groupId>org.apache.struts</groupId>
		<artifactId>struts2-dojo-plugin</artifactId>
		<version>${struts-version}</version>
	</dependency>
......
```

To use this tag:

- Add: *&lt;%@ taglib prefix="sx" uri="/struts-dojo-tags" %&gt;* to your page.
- The [head](http://struts.apache.org/2.x/docs/dojo-head.html "dojo head") tag must be included on the page, which can be configured for performance or debugging purposes.
- If the parseContent parameter for the [head](http://struts.apache.org/2.x/docs/dojo-head.html "dojo head") tag is false (it is false by default), then the **id** tag is required.

 

```
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<sx:head/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Save Contact</title>
<script type="text/javascript" src="scripts/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
	
</script>
</head>
<body>
	<div align="center">
		<h1>
			<font color="red">Save Contact</font>
		</h1>

		<s:form action="saveContact">
			<s:textfield name="contact.firstName" label="ContactFirstName"></s:textfield>
			<s:textfield name="contact.lastName" label="ContactLastName"></s:textfield>
			<sx:datetimepicker name="contact.birthDate" label="ContactBirthDate"
				displayFormat="yyyy-MM-dd"></sx:datetimepicker>
			<s:submit value="submit"></s:submit>
		</s:form>
	</div>
</body>
</html>
```

 

![](http://dl.iteye.com/upload/attachment/0073/5768/c48cdbcf-a342-3ab8-99a7-251c72dc4a68.png)

详细文档说明可以参考[http://struts.apache.org/2.x/docs/datetimepicker.html](http://struts.apache.org/2.x/docs/datetimepicker.html)