Step-by-Step struts1 (6) custom Converter

Source: Internet
Author: User

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)



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.