The type conversion provided by struts is limited. If we forcibly use a type conversion without it, an error will occur. Take the date type as an example:
Org. Apache. Catalina. Core. standardwrappervalve invoke severe: servlet. Service () for servlet action threw exceptionjava. Lang. Failed: cannot invoke com. tgb. struts1.typecovertactionform. setutildate-argument Type Mismatch
Here we will create a java. util. Date converter.
Servlet Converter
The data converter 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 to 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, Set 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
<H4> test the struts converter </H4> <form action = "typeconvert. do "method =" Post "> int value: <input type =" text "name =" intvalue "/> <br/> double value: <input type = "text" name = "doublevalue"/> <br/> Boolean value: <input type = "text" name = "booleanvvlue"/> <br/> JAVA. SQL. date: <input type = "text" name = "sqldate"/> <br/> JAVA. util. date: <input type = "text" name = "utildate"/> <br/> <input type = "Submit" value = "Submit"/> <br/> </form>
Result
Plugin version
Plugin
package com.tgb.struts1;import javax.servlet.ServletException;import org.apache.commons.beanutils.ConvertUtils;import org.apache.struts.action.ActionServlet;import org.apache.struts.action.PlugIn;import org.apache.struts.config.ModuleConfig;public class UtilDateConverterPlugin implements PlugIn {public void destroy() {// TODO Auto-generated method stub}public void init(ActionServlet servlet, ModuleConfig config)throws ServletException {ConvertUtils.register(new UtilDateConverter(), java.util.Date.class);}}
Configuration File
Put the following in Struts-config:
<plug-in className="com.tgb.struts1.UtilDateConverterPlugin" />
Test Results
Summary
The extensible benefits of abstract programming are introduced here. The two custom converter processes in this blog are basically the same: the implementation interface writes the converter and changes the configuration file to ensure registration can be completed upon Tomat loading.
For more related blogs, go to the Summary of progressive struts1 (8)