The type conversions provided by struts are limited, and if we forcibly use the type conversions that are not available, an error occurs, taking the date type as an example:
Org.apache.catalina.core.StandardWrapperValve invoke
severity: Servlet.service () for Servlet action threw exception
Java.lang.IllegalArgumentException:Cannot invoke
com.tgb.struts1.TypeCovertActionForm.setUtilDate- Argument type mismatch
Here we set up the Java.util.Date converter.
Servlet version Converter
Data converter, the code is as follows:
Package com.tgb.struts1;
Import Java.text.SimpleDateFormat;
Import java.util.Date;
Import org.apache.commons.beanutils.ConversionException;
Import Com.sun.org.apache.commons.beanutils.Converter;
public class Utildateconverter implements Org.apache.commons.beanutils.Converter {public
Object convert (class Type, Object value {
if (value instanceof Date) {return
(value);
}
Date Date=null;
if (value instanceof string) {
try {
date=new simpledateformat ("Yyy-mm-dd"). Parse ((String) value);
catch (Exception e) {
e.printstacktrace ();
}
}
Return date
}
}
Servlet
In order to register the above converter in Convertutils:
Package
Com.tgb.struts1;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Org.apache.commons.beanutils.ConvertUtils;
public class Utilservlet extends HttpServlet {public
void init () throws Servletexception
{
Convertutils.register (New Utildateconverter (), java.util.Date.class);
}
Configuration file
Add the configuration in Web.xml because the converter is loaded at Tomcat startup, so here you set the Load-on-startup to a value greater than 0:
<servlet>
<servlet-name>UtilServlet</servlet-name>
<servlet-class> com.tgb.struts1.utilservlet</servlet-class>
<load-on-startup>10</load-on-startup>
</servlet>
Test
Jsp
Results