JAVA SPRINGMVC Converters

Source: Internet
Author: User

One, sometimes springmvc to provide us with the data conversion can not convert all types such as the string type conversion date type, this time we need to customize the converter to help Springmvc transform the type we need.

Two, 1) define our converters:

1 Package Jd.com.contronller.jd.com.convert;2 3 import Org.springframework.core.convert.converter.Converter;4 5 import java.text.ParseException;6 import Java.text.SimpleDateFormat;7 import java.util.Date;8 9  Public classCustomstringtodate Implements Converter<string,date> {Ten @Override One      PublicDate Convert (String s) { A         Try { -Date date=NewSimpleDateFormat ("YYYY-MM-DD HH:mm:ss"). Parse (s); -             returndate; the}Catch(ParseException e) { - e.printstacktrace (); -         } -         return NULL; +     } -}

2) Add our converters in the SPRINGMVC configuration file:

1<!--custom converters Note the reference in the annotation driver2<bean id="Conversionservice"3     class="Org.springframework.format.support.FormattingConversionServiceFactoryBean">4<property name="Converters">5<Set>6<beanclass="jd.com.contronller.jd.com.convert.CustomStringToDate"/>7</Set>8</property>9</bean>

3) Need to note: Add our Converter ID in the note driver

1 <mvc:annotation-driven conversion-service="conversionservice"  />

Because the converter is processor adapter processing, if we do not use annotations, the traditional reference is as follows:

<?xmlversion="1.0"encoding="UTF-8"? ><beansxmlns="Http://www.springframework.org/schema/beans"Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"Xmlns:context="Http://www.springframework.org/schema/context"Xmlns:dubbo="Http://code.alibabatech.com/schema/dubbo"Xmlns:mvc="Http://www.springframework.org/schema/mvc"xsi:schemalocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp//Www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp//Code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/schema/dubbo/dubbo.xsdhttp//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsd"><!--scan a class with controller annotations--<context:component-scanbase-package="Cn.itcast.springmvc.controller"/> <!--converter configuration-<beanid="Conversionservice"        class="Org.springframework.format.support.FormattingConversionServiceFactoryBean"> <propertyname="Converters"> <Set> <beanclass="jd.com.contronller.jd.com.convert.CustomStringToDate"/> </Set> </property> </bean> <!--custom Webbinder-<beanid="Custombinder"    class="Org.springframework.web.bind.support.ConfigurableWebBindingInitializer"> <propertyname="Conversionservice"ref="Conversionservice"/> </bean> <!--Note Adapter--<beanclass="Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"><propertyname= "Webbindinginitializer" ref= "Custombinder" ></property></bean> <!--Note Processor Mapper-<beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

Note the following:

Front-end page to display the date format you need to define the following tags:

1  <td><fmt:formatdate value="${n.create_time} " pattern="  YYYY-MM-DD HH:mm:ss" /> </td>

Need to pay attention to the format needs and the subsequent converter class format consistent, otherwise error!!!!

JAVA SPRINGMVC Converters

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.