Spring MVC 自定义类型转换器

新建一个自定义转换器
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 class EmployeeConverter implements Converter<String, Employee> { @Override
public Employee convert(String source) {
if(source != null){
String [] vals = source.split("-");
//GG-gg@atguigu.com-0-105
if(vals != null && vals.length == ){
String lastName = vals[];
String email = vals[];
Integer gender = Integer.parseInt(vals[]);
Department department = new Department();
department.setId(Integer.parseInt(vals[])); Employee employee = new Employee(null, lastName, email, gender, department);
System.out.println(source + "--convert--" + employee);
return employee;
}
}
return null;
} }
配置xml
<!-- 配置 ConversionService -->
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<ref bean="employeeConverter"/> <!-- 自定义的类名,首字母小写 -->
</set>
</property>
</bean> <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
Spring MVC 自定义类型转换器的更多相关文章
- java MVC 自定义类型转换器(Formatter、AnnotationFormatterFactory)
下面一个事例,是将传入的一个身份证号,转换成一个对象(提取身份证号的地址.出身日期.性别等) 实体类 Person 有三个字段如下: String province; //地址 Date birthd ...
- Ⅵ.spring的点点滴滴--自定义类型转换器
承接上文 自定义类型转换器 .net篇(环境为vs2012+Spring.Core.dll v1.31) public class CustomeConverter : TypeConverter{ ...
- 《SpringMVC从入门到放肆》十二、SpringMVC自定义类型转换器
之前的教程,我们都已经学会了如何使用Spring MVC来进行开发,掌握了基本的开发方法,返回不同类型的结果也有了一定的了解,包括返回ModelAndView.返回List.Map等等,这里就包含了传 ...
- 自定义类型转换器 及 使用 ServletAPI 对象作为方法参数
自定义类型转换器使用场景: jsp 代码: <!-- 特殊情况之:类型转换问题 --> <a href="account/deleteAccount?date=2018- ...
- SpringMVC自定义类型转换器
SpringMVC 自定义类型转换器 我们在使用SpringMVC时,常常需要把表单中的参数映射到我们对象的属性中,我们可以在默认的spring-servlet.xml加上如下的配置即可做到普通数据 ...
- springmvc中如何自定义类型转换器
package com.hope.utils;import org.springframework.core.convert.converter.Converter;import org.spring ...
- 0023SpringMVC自定义类型转换器
页面录入的字符串:2019/12/05可以映射到实体的日期属性上,但是如果是录入2019-12-05就会报错400 bad request,想要以2019-12-05日期格式的方式映射到实体的日期属性 ...
- 阶段3 3.SpringMVC·_02.参数绑定及自定义类型转换_6 自定义类型转换器代码编写
mvc是基于组件的方式 类型转换的接口Converter,想实现类型转换,必须实现这个接口 Ctrl+N搜索 converter 这是一个接口类 它有很多的实现类.S是字符串.后面T是指要转换类型 新 ...
- springmvc:自定义类型转换器代码编写
字符串转换日期: 1.自定义一个类 /** * 字符串转换日期 */ public class StringToDateConverter implements Converter<String ...
随机推荐
- 将一个list转成json数组-晚上坐49路回去打卡
- JSONObject JSONArray json字符串 HashMap ArryList 在java开发中用到的数据结构
1.JSONObject 长成这样的: { "key1":value1, "key2":value2, "key3":value3} ...
- python 常见的内置函数
内置函数 接下来,我们就一起来看看python里的内置函数.截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就是python提供给你直接可以拿来使用的所有函数.这 ...
- 开源泛域名服务xip.io部署试用
xip.io 是一个很方便的泛域名服务,类似的有一个xip.name 的开源实现 下载 go get github.com/peterhellberg/xip.name 启动 二进制包在GOPATH/ ...
- call和apply的意义和区别
区别在于 call 的第二个参数可以是任意类型,而apply的第二个参数必须是数组 如 func.call(func1,var1,var2,var3)对应的apply写法为:func.apply(f ...
- SQL Server系统表介绍与使用
关于SQL Server数据库的一切信息都保存在它的系统表格里.我怀疑你是否花过比较多的时间来检查系统表格,因为你总是忙于用户表格.但是,你可能需要偶尔做一点不同寻常的事,例如数据库所有的触发器.你可 ...
- zstack快速安装文档
1.环境准备 1.1 准备软件工具 系统镜像 ZStack-x86_64-DVD-2.1.1.514.iso Zstack安装包 ZStack-installer-2.1.1.514.bin http ...
- weexpack 使用
weexpack 的github地址:https://github.com/weexteam/weex-pack weex-toolkit: 初始化的项目是针对开发单个 Weex 页面而设计的,也就是 ...
- 使用 extract-text-webpack-plugin 报错:Error: Chunk.entry was removed. Use hasRuntime()
问题:使用 extract-text-webpack-plugin 报错:Error: Chunk.entry was removed. Use hasRuntime() 解决:先运行npm unin ...
- appium 中文API 集
参考:https://testerhome.com/topics/3711 根据appium 1.4.13.1版本整理,1.5弃用了find by name 所以更新了下如有错误请多多指正谢谢@lyl ...