Layered progressive Struts1 (vi) Custom converters

Source: Internet
Author: User
Tags date

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

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.