【Spring学习笔记-MVC-8】SpringMVC之类型转换Converter
1. 摘要
- Converter接口 :使用最简单,最不灵活;
- ConverterFactory接口 :使用较复杂,比较灵活;
- GenericConverter接口 :使用最复杂,也最灵活;

- 定义转换类,实现Converter<S,T>接口;
- 装配自定义的conversionService;
2. 定义类型转换类
StringToPersonConverter.java

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;}}
3. 装配自定义的conversionService

<?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>
4. 控制器



附件列表
【Spring学习笔记-MVC-8】SpringMVC之类型转换Converter的更多相关文章
- 【Spring学习笔记-MVC-9】SpringMVC数据格式化之日期转换@DateTimeFormat
作者:ssslinppp 1. 摘要 本文主要讲解Spring mvc数据格式化的具体步骤: 并讲解前台日期格式如何转换为java对象: 在之前的文章<[Spring学习笔记-MVC ...
- 【Spring学习笔记-MVC-4】SpringMVC返回Json数据-方式2
<Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...
- 【Spring学习笔记-MVC-3】SpringMVC返回Json数据-方式1
<Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...
- 3.《Spring学习笔记-MVC》系列文章,讲解返回json数据的文章共有3篇,分别为:
转自:https://www.cnblogs.com/ssslinppp/p/4528892.html 概述 在文章:<[Spring学习笔记-MVC-3]SpringMVC返回Json数据-方 ...
- 1.《Spring学习笔记-MVC》系列文章,讲解返回json数据的文章共有3篇,分别为:
转自:https://www.cnblogs.com/ssslinppp/p/4528892.html [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://w ...
- 2.《Spring学习笔记-MVC》系列文章,讲解返回json数据的文章共有3篇,分别为:
转自:https://www.cnblogs.com/ssslinppp/p/4528892.html 个人认为,使用@ResponseBody方式来实现json数据的返回比较方便,推荐使用. 摘要 ...
- 【Spring学习笔记-MVC-6】SpringMVC 之@RequestBody 接收Json数组对象
作者:ssslinppp 1. 摘要 程序流程: 前台使用ajax技术,传递json字符串到后台: 后台使用Spring MVC注解@RequestBody 接受前台传递的json字符串, ...
- 【Spring学习笔记-MVC】Spring MVC之多文件上传 (zhan)
http://www.cnblogs.com/ssslinppp/p/4607330.html (zhan)
- 【Spring学习笔记-MVC-3.1】SpringMVC返回Json数据-方式1-扩展
<Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...
随机推荐
- python 数据科学
参考资料: https://www.yiibai.com/python_data_science/python_bubble_charts.html
- struts2 正确配置通配符方式访问,报错解决
今天遇到正确配置通配符访问action的方法,但是还是报错,原因struts 2.3 以后会内部会验证是否允许该方法,而我用的刚好是2.5的版本 要action配置中加上<allowed-met ...
- Spring Data JPA 复杂/多条件组合分页查询
推荐视频: http://www.icoolxue.com/album/show/358 public Map<String, Object> getWeeklyBySearch(fina ...
- 《Python》 内置函数补充、匿名函数、递归初识
一.内置函数补充: 1.数据结构相关(24): 列表和元祖(2):list.tuple list:将一个可迭代对象转化成列表(如果是字典,默认将key作为列表的元素). tuple:将一个可迭代对象转 ...
- 相同类型的对象不能互相转换:java.lang.ClassCastException: com.anhoo.po.UserPo cannot be cast to com.anhoo.po.UserPo
@PostMapping("/findone") public String getone(UserVo userVo) throws IllegalAccessException ...
- 写在连载之前——DIY微型操作系统篇
这个博客开了这么久都没写过什么东西.可能是因为我想写的东西在网上都能找得到,所以自己也懒得去写了. 但是这次当我在看<30天自制操作系统>这本书的时候发现,如果不用作者原版的光盘软件,要自 ...
- SWIFT Enumeration(2)
之前记录了Swift Enumeration(1),这篇算是它的延续吧,继续说下Enumeration,看以下定义 enum TrainStatus { case OnTime case Delay( ...
- HDU 2813
http://acm.hdu.edu.cn/showproblem.php?pid=2813 裸二分图最优匹配,需要用两个map把武将名字映射到点的序号上 #include <iostream& ...
- RCNN、SPP-net、Fast-RCNN和Faster-RCNN
RCNN RCNN (Regions with CNN features) 的核心思想是把图像划分成N(2000)个独立的区域,分别提取每个区域的CNN特征,然后把这些特征使用SVM等分类器进行结果预 ...
- Android的界面设计工具——DroidDraw
软件名称:DroidDraw 软件大小:489KB(Windows版本) 支持系统:Mac OS X/Windows/Linux 下载地址:http://code.google.com/p/droid ...