SpringMVC学习(六)-自定义类型转换器
来源:互联网 发布:网络融资平台靠谱吗 编辑:程序博客网 时间:2024/05/18 00:46
完成实现自定义类型转换器的重点:
1)编写类型转换器
2)配置配置文件(定义转换器)
3)控制层代码
1.定义的类型转换器
package com.springmvc.controller;import org.springframework.core.convert.converter.Converter;import org.springframework.core.convert.converter.ConverterFactory;import org.springframework.stereotype.Component;import com.springmvc.crud.entiry.Department;import com.springmvc.crud.entiry.Employee;@Componentpublic class EmployeeConverter implements Converter<String,Employee> { @Override public Employee convert(String source) { if(source!=null) { String[] vals=source.split("-"); if(vals!=null&&vals.length==4) { String lastname=vals[0]; String email=vals[1]; Integer gender=Integer.parseInt(vals[2]); Department department=new Department(); department.setId(Integer.parseInt(vals[3])); Employee employee=new Employee(null,lastname,email,gender,department); System.out.println(source+" --convert--"+employee); return employee; } } return null; }}
2.控制层
package com.springmvc.controller;import org.springframework.core.convert.converter.Converter;import org.springframework.core.convert.converter.ConverterFactory;import org.springframework.stereotype.Component;import com.springmvc.crud.entiry.Department;import com.springmvc.crud.entiry.Employee;@Componentpublic class EmployeeConverter implements Converter<String,Employee> { @Override public Employee convert(String source) { if(source!=null) { String[] vals=source.split("-"); if(vals!=null&&vals.length==4) { String lastname=vals[0]; String email=vals[1]; Integer gender=Integer.parseInt(vals[2]); Department department=new Department(); department.setId(Integer.parseInt(vals[3])); Employee employee=new Employee(null,lastname,email,gender,department); System.out.println(source+" --convert--"+employee); return employee; } } return null; }}
3.配置文件
package com.springmvc.controller;import org.springframework.core.convert.converter.Converter;import org.springframework.core.convert.converter.ConverterFactory;import org.springframework.stereotype.Component;import com.springmvc.crud.entiry.Department;import com.springmvc.crud.entiry.Employee;@Componentpublic class EmployeeConverter implements Converter<String,Employee> { @Override public Employee convert(String source) { if(source!=null) { String[] vals=source.split("-"); if(vals!=null&&vals.length==4) { String lastname=vals[0]; String email=vals[1]; Integer gender=Integer.parseInt(vals[2]); Department department=new Department(); department.setId(Integer.parseInt(vals[3])); Employee employee=new Employee(null,lastname,email,gender,department); System.out.println(source+" --convert--"+employee); return employee; } } return null; }}
4.jsp页面
<form action="testConversionServiceConverer" method="POST"> <!-- lastname-email-gender-department.id --> Employee:<input type="text" name="employee"/> <input type="submit" value="Submit"/></form>
阅读全文
0 0
- SpringMVC学习(六)-自定义类型转换器
- springmvc学习笔记(22)——自定义类型转换器
- 学习SpringMVC(十七)之自定义类型转换器
- SpringMVC 自定义类型转换器
- springMVC 自定义类型转换器
- SpringMVC 自定义类型转换器
- SpringMVC自定义类型转换器
- springmvc的自定义类型转换器
- springMVC的自定义类型转换器
- 学习springmvc的第十三天(自定义类型转换器ConversionServiceFactoryBean)
- springmvc自定义类型转换器【字符串--->POJO类】
- springmvc之自定义类型转换器ConversionServiceFactoryBean
- SpringMVC——自定义类型转换器
- springMvc(4)---类型转换器
- Struts2系统学习(8)自定义类型转换器
- Struts2学习笔记之自定义类型转换器
- Struts2学习笔记之自定义类型转换器
- struts2学习笔记---------自定义类型转换器
- poj 4115:鸣人和佐助
- Qt5 文件编码
- 系统地讲述Cookie与Session机制
- 三重循环寻找最长连续字符串相加和为K值倍数的长度
- Leetcode 289. Game of Life
- SpringMVC学习(六)-自定义类型转换器
- Netty学习-02
- Shader的纹理
- XML文件解析
- java创建线程的三种方式及其对比
- poj 4102:宠物小精灵之收服
- 使用SVN进行版本控制
- toString()模板语法研究
- 【学习笔记四】- 用js实现的一些数组操作和算法