

package com.ll.model;import org.springframework.core.convert.converter.Converter;/*** @author ssslinppp* Spring MVC数据转换-简单示例* 将形如:“zhangSan:888”的字符串转换为Person对象**/public class StringToPersonConverter implements Converter<String,Person>{public Person convert(String source) {Person p1 = new Person();if(source != null){String[] items = source.split(":");p1.setUsername(items[0]);p1.setPasswd(items[1]);}return p1;}}

<?xml version="1.0" encoding="UTF-8" ?><beans xmlns="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:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><!-- 扫描web包,应用Spring的注解 --><context:component-scan base-package="com.ll.web"/><mvc:annotation-driven conversion-service="conversionService"/><bean id="conversionService"class="org.springframework.context.support.ConversionServiceFactoryBean"><property name="converters"><list><bean class="com.ll.model.StringToPersonConverter" /></list></property></bean><!-- 配置视图解析器,将ModelAndView及字符串解析为具体的页面,默认优先级最低 --><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"p:viewClass="org.springframework.web.servlet.view.JstlView"p:prefix="/jsp/"p:suffix=".jsp" /></beans>



【Spring学习笔记-MVC-8】SpringMVC之类型转换
原文:http://www.cnblogs.com/ssslinppp/p/4598102.html