struts2自定义数据类型转换器------日期转换器

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">&lt;&lt;重新转换</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>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值