SpringMVC学习(5):自己设置数据转换器
前言
在前面的章节我们学习过使用SpringMVC框架进行web开发,前台页面传输的数据会由HandlerAdapter组件自动封装到业务方法的参数中。
在http表单中的所有请求参数都是String类型的,假如业务参数是String或者者int类型,HandlerAdapter会自动完成数据转换,但是假如参数是其余数据类型,比方Date类型,HandlerAdapter是不会将String类型转换为Date类型的,我们需要实现Converter接口来协助SpringMVC完成数据类型的转换。
实现
1、创立DateConverter转换器
import org.springframework.core.convert.converter.Converter;public class DateConverter implements Converter<String,Date>{ private String pattern; public DateConverter(String pattern){ this.pattern = pattern; } public Date convert(String source) { // TODO Auto-generated method stub SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); try { return simpleDateFormat.parse(source); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }}
DateConverter转换器实现了org.springframework.core.convert.converter.Converter接口,将String类型的数值转换为Date类型。
2、配置springmvc.xml中的conversionService bean
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <list> <!-- 日期转换器 --> <bean class="com.jex.utils.DateConverter"> <constructor-arg type="java.lang.String" value="yyyy-MM-dd"/> </bean> </list> </property> </bean> <mvc:annotation-driven conversion-service="conversionService"/>
注意:bean的类名称必需是org.springframework.context.support.ConversionServiceFactoryBean。bean必需包含一个converters属性,它将列出在应用程序中用到的所有定制Converter。将我们自己设置的DateConverter增加到converters中,通过有参构造函数创立DateConverter。同时annotation-driven元素的conversion-service属性赋bean名称。
3、创立业务方法
@RequestMapping(value="/testDateConverter") @ResponseBody public String testDateConverter(Date date){ return date.toString(); }
4、创立addDate.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head><body> <form action="testDateConverter" method="post"> 请输入日期:<input type="text" name="date"/>(yyyy-MM-dd)<br/> <input type="submit" value="提交"/> </form></body></html>
5、运行
image
image
除了Date类型的转换,我们还可以自己设置其它的数据格式,比方注册一个user,通过转换器将前台页面输入的”id-name-age”形式的String类型的数据直接转换为user对象。
1、创立User实体类
public class User { private int id; private String name; private int age; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
2、创立UserConverter转换器
import org.springframework.core.convert.converter.Converter;public class UserConverter implements Converter<String,User>{ public User convert(String source) { // TODO Auto-generated method stub String[] args = source.split("-"); User user = new User(); user.setId(Integer.parseInt(args[0])); user.setName(args[1]); user.setAge(Integer.parseInt(args[2])); return user; }}
3、配置springmvc.xml中的conversionService bean
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <list> <!-- 日期转换器 --> <bean class="com.jex.utils.DateConverter"> <constructor-arg type="java.lang.String" value="yyyy-MM-dd"/> </bean> <!--User转换器 --> <bean class="com.jex.utils.UserConverter"></bean> </list> </property> </bean> <mvc:annotation-driven conversion-service="conversionService"/>
4、创立业务方法
@RequestMapping(value="/testUserConverter") @ResponseBody public String testUserConverter(User user){ return user.toString(); }
5、创立addUser.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head><body> <form action="testUserConverter" method="post"> 客户信息:<input type="text" name="student"/><font style="font-size:13px">(id-name-age)</font><br/> <input type="submit" value="提交"/> </form></body></html>
6、运行
image
image
源码
链接:
https://pan.baidu.com/s/191wnUwW0a8AOeZnhOSWH5A
提取码:1v5q
image
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » SpringMVC学习(5):自己设置数据转换器