---
title: "struts格式化问题"
author: "loveseaside"
date: "2010-05-11T10:09:08Z"
tags: ["Apache", "Bean", "DB2", "SQL", "Struts"]
categories: ["企业架构"]
slug: "struts"
draft: false
lastmod: "2010-05-11T10:15:01Z"
id: 663645
url: "https://loveseaside.iteye.com/blog/663645"
---

日期格式化  
db2的字段类型定义为TIMESTAMP 显示毫秒，（hibernate保存数据时候也是如此）  
 2010-05-11 10:06:30.068  2010-05-20 00:00:00.0  
用struts bean:write标签进行格式化输出  
&lt;bean:write name="" property="" format="yyyy"/&gt;             2010  
&lt;bean:write name="" property="" format="yyyy-MM-dd "/&gt;      2010-05-20  
&lt;bean:write name="" property="" format="yyyy-MM-dd HH:mm:ss"/&gt; 2010-05-20 00:00:00.0

数字格式  
用struts bean:write标签进行格式化输出  
&lt;bean:write name="" property="" format="0000"/&gt;  
不足四位前补充0；0008如&lt;bean:write name="" property="" format="0000"/&gt; 显示为0008，而不是8  
&lt;bean:write name="" property="" format="###0"/&gt;  
不足四位前不补充0；0008如&lt;bean:write name="" property="" format="###0"/&gt; 显示为8，而不是0008  
&lt;bean:write name="" property="" format="###0.00"/&gt;  
#前面不补充0，小数点后不足2位补0; 0008.00如&lt;bean:write name="" property="" format="###0.00"/&gt; 显示为8.00

&lt;bean:write name="" property="" format="#,###"/&gt;

,表示分割 8000如&lt;bean:write name="" property="" format="#,###"/&gt; 显示为8,000  
项目中的变量大多以同一格式输出，可以在struts-config.xml中的message-resources节点统一进行定义，如：org.apache.struts.taglib.bean.format.float=0,000.00

实际处理时页面上定义的format拥有较高的优先级。

附录：

序号          格式化参数                                                                       适用类型  
1    org.apache.struts.taglib.bean.format.sql.timestamp             java.sql.Timestamp  
2    org.apache.struts.taglib.bean.format.sql.date                      java.sql.Date  
3    org.apache.struts.taglib.bean.format.sql.time                       java.sql.Time  
4    org.apache.struts.taglib.bean.format.date                            java.util.Date  
5    org.apache.struts.taglib.bean.format.int                             Byte，Short，Integer，Long，BigInteger  
6    org.apache.struts.taglib.bean.format.float                          Float，Double，BigDecimal