自定义时间转化器
1,建立一个MyTypeConvertor 类继承Converter(Converter类下有各种类型转化的类)
package com.platform_easyuiSSH.struts1.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.beanutils.Converter;
public class MyTypeConvertor implements Converter{
@SuppressWarnings({ "rawtypes", "unchecked" })
public Object convert( Class arg0, Object arg1) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date d = null;
try {
d = sdf.parse((String)arg1);
} catch (ParseException e) {
e.printStackTrace();
}
return d;
}
}
2,注册该类到Struts框架,需要去web.xml中配置RegAction
package com.platform_easyuiSSH.struts1.util;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.commons.beanutils.ConvertUtils;
/**
* 重构action,注册自定义类型实现类
* @author Administrator
*
*/
public class RegAction extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
public void init(ServletConfig config) throws ServletException {
//注册转换器
ConvertUtils.register(new MyTypeConvertor(), java.util.Date.class);
}
}