package cfl.convert.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import javax.servlet.http.HttpServlet; import org.apache.commons.beanutils.Converter; /* * 初始化转换器-陈方林-2014年10月15日 */ public class personalConvert implements Converter { @Override public Object convert(Class type, Object value) { // TODO Auto-generated method stub if(value instanceof Date){ return value; } if(value instanceof String){ try { return new SimpleDateFormat("yyyy-MM-dd").parse((String)value); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; } }
package cfl.convert.util; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import org.apache.commons.beanutils.ConvertUtils; public class personalConvertServlet extends HttpServlet { @Override public void init() throws ServletException { // TODO Auto-generated method stub System.out.print("success"); ConvertUtils.register(new personalConvert(), java.util.Date.class); } }
<servlet> <servlet-name>personConvert</servlet-name> <servlet-class>cfl.convert.util.personalConvertServlet</servlet-class> <load-on-startup>3</load-on-startup> </servlet>
<form-beans> <form-bean name="dynaForm" type="org.apache.struts.action.DynaActionForm"> <form-property name="username" type="java.lang.String"></form-property> <form-property name="age" type="java.lang.Integer"></form-property> </form-bean> </form-beans>
原文:http://blog.csdn.net/cfl20121314/article/details/40897659