Action中最终需要的日期类型为:
Date sqlDate;
Time sqlTime;
java.util.Date utilDate;
自定义转换器存在必然性?若用默认的日期转换器,输入时间12:00会跑错。
以下代码自定义了一个日期转换器:
public class DateTimeConvertor extends DefaultTypeConverter {
private DateFormat[] dateFormat = { new SimpleDateFormat("yyyy-MM-dd"),
new SimpleDateFormat("yyyy/MM/dd"),
new SimpleDateFormat("yy-MM-dd"), };
private DateFormat[] timeFormat = { new SimpleDateFormat("HH:mm:ssss"),
new SimpleDateFormat("HH:mm"), };
@Override
@SuppressWarnings("all")
public Object convertValue(Map context, Object value, Class toType) {
if (toType.equals(java.sql.Date.class)) {
String[] parameterValues = (String[]) value;
for (DateFormat format : dateFormat)
try {
return new java.sql.Date(format.parse(parameterValues[0])
.getTime());
} catch (ParseException e) {
}
} else if (toType.equals(java.sql.Time.class)) {
String[] parameterValues = (String[]) value;
for (DateFormat format : timeFormat)
try {
return new java.sql.Time(format.parse(parameterValues[0])
.getTime());
} catch (ParseException e) {
}
} else if (toType.equals(java.util.Date.class)) {
String[] parameterValues = (String[]) value;
for (DateFormat format : dateFormat)
try {
return format.parse(parameterValues[0]);
} catch (ParseException e) {
}
} else if (toType.equals(String.class)) {
if (value instanceof java.sql.Date) {
} else if (value instanceof java.sql.Time) {
} else if (value instanceof java.util.Date) {
return dateFormat[0].format((java.util.Date) value);
}
}
return super.convertValue(context, value, toType);
}
}
自定义的日期转换器需要配置(xwork-conversion.properties):定义哪些类型需要使用自定义的类型转换器
java.sql.Date = com.belstar.convertor.DateTimeConvertor
java.sql.Time = com.belstar.convertor.DateTimeConvertor
java.util.Date =com.belstar.convertor.DateTimeConvertor
需要转换的页面字段设计:convertor.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="struts"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<struts:head theme="simple" />
<style type="text/css">
body,td {
font-size: 12px;
}
</style>
</head>
<body>
<struts:form action="convertor"> <!-- action 指定对应的action,和struts.xml文件中action元素的name属性对应-->
<struts:label label="转换器" />
<struts:textfield name="sqlDate" label="SQL Date:" />
<struts:textfield name="sqlTime" label="SQL Time:" />
<struts:textfield name="utilDate" label="Util Date:" />
<struts:submit value=" 提交 " method="convert" /> <!-- 指定执行action中的哪个方法-->
</struts:form>
</body>
</html>
转换后的显示也页面:convertorSuccess.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="struts"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<struts:head theme="simple" />
<style type="text/css">
body,td {
font-size: 12px;
}
</style>
</head>
<body>
java.sql.Date: <struts:property value="sqlDate" /> <br/> <!-- struts2标签能够直接显示日期时间类型-->
java.sql.Time: <struts:property value="sqlTime" /> <br/>
java.util.Date: <struts:property value="utilDate" /> <br/>
<br/>
<a href="convertor.action"><<重新转换</a>
</body>
</html>
关键一步:为action类配置日期转换类
<struts>
<package name="main" extends="struts-default">
<action name="convertor"
class="com.belstar.action.ConvertorAction" converter="com.belstar.convertor.DateTimeConvertor"> <!--表明ConvertorAction类使用的是DateTimeConvertor日期转换器-->
<result name="input">/convertor.jsp</result>
<result name="success">/convertorSuccess.jsp</result>
</action>
</package>
</struts>