springmvc: Custom coding type converter

String Conversion Date:

1. a custom class

1  / * 
2  * Date string conversion
 . 3   * / 
. 4  public  class StringToDateConverter the implements Converter <String, a Date> {
 . 5  
. 6      / ** 
. 7       * Source String String incoming come
 . 8       * @param Source
 . 9       * @return 
10       * / 
. 11      @Override
 12 is      public a Date Convert (String Source) {
 13 is          // Analyzing 
14          IF (Source == null ) {
 15              // exception thrown run 
16             the throw  new new a RuntimeException ( "Please incoming data" );
 . 17          }
 18 is          DateFormat DF = new new the SimpleDateFormat ( "the MM-dd-YYYY" );
 . 19          the try {
 20 is              // convert string date 
21 is              return df.parse (Source) ;
 22 is          } the catch (a ParseException E) {
 23 is              the throw  new new a RuntimeException ( "data type conversion errors" );
 24          }
 25  
26 is      }
 27 }

Second, springmvc.xml configure custom type converter

1      <! - configure a custom type converter ->
 2      <the bean ID = "ConversionService" class = "org.springframework.context.support.ConversionServiceFactoryBean">
 . 3          <Property name = "Converters">
 . 4              <SET>
 . 5                  <the bean class = "cn.flypig666.utils.StringToDateConverter"> </ the bean>
 . 6              </ SET>
 . 7          </ Property>
 . 8      </ the bean>
 . 9  
10      <-! SpringMVC open support frame annotation ->
 11      < mvc: annotation-driven conversion-service = "conversionService" />

 

Guess you like

Origin www.cnblogs.com/flypig666/p/11517536.html