Spring MVC Custom type converter

Source: Internet
Author: User

Creating a new Custom converter

import Org.springframework.core.convert.converter.converter;import org.springframework.stereotype.Component; Import Com.atguigu.springmvc.crud.entities.department;import com.atguigu.springmvc.crud.entities.employee;@ Component Public classEmployeeconverter implements Converter<string, employee>{@Override PublicEmployee Convert (String source) {if(Source! =NULL) {String [] vals= Source.split ("-"); //[email protected]            if(Vals! =NULL&& Vals.length = =4) {String lastName= vals[0]; String Email= vals[1]; Integer Gender= Integer.parseint (vals[2]); Department Department=NewDepartment (); Department.setid (Integer.parseint (vals[3])); Employee Employee=NewEmployee (NULL, LastName, email, Gender, department); System. out. println (source +"--convert--"+employee); returnemployee; }        }        return NULL; }}

Configuration XML

<!--configuration Conversionservice--<bean id="Conversionservice"        class="Org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="Converters"> <Set> <refbean="Employeeconverter"/> <!--custom class name, first lowercase--</Set> </property> </bean>

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

Spring MVC Custom type converter

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.