SpringMVC之类型转换Converter

https://www.cnblogs.com/ssslinppp/p/4598102.html


1. 摘要


在spring 中定义了3中类型转换接口,分别为:
  1.   Converter接口              :使用最简单,最不灵活;
  2.   ConverterFactory接口  :使用较复杂,比较灵活;
  3.   GenericConverter接口 :使用最复杂,也最灵活;
可参考链接:  http://blog.csdn.net/renhui999/article/details/9837897 。看看他们的介绍;
本文仅介绍 Converter接口的使用。

这里仅仅是一个简单的 类型转换 示例,重点在于说明SpringMVC类型转换的方法、配置及流程;

基本功能说明:
1. 前台传递的数据格式形如:“zhangSan:888”;

2. 将 “zhangSan:888” 字符串转换为Person对象;

具体过程:
  1. 定义转换类,实现Converter<S,T>接口;
  2.  装配自定义的conversionService;

2. 定义类型转换类


StringToPersonConverter.java

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


3. 装配自定义的conversionService


在Spring的web上下文中配置:

spring-servlet.xml

   
   
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  10. http://www.springframework.org/schema/mvc
  11. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
  12. <!-- 扫描web包,应用Spring的注解 -->
  13. <context:component-scan base-package="com.ll.web"/>
  14. <mvc:annotation-driven conversion-service="conversionService"/>
  15. <bean id="conversionService"
  16. class="org.springframework.context.support.ConversionServiceFactoryBean">
  17. <property name="converters">
  18. <list>
  19. <bean class="com.ll.model.StringToPersonConverter" />
  20. </list>
  21. </property>
  22. </bean>
  23. <!-- 配置视图解析器,将ModelAndView及字符串解析为具体的页面,默认优先级最低 -->
  24. <bean
  25. class="org.springframework.web.servlet.view.InternalResourceViewResolver"
  26. p:viewClass="org.springframework.web.servlet.view.JstlView"
  27. p:prefix="/jsp/"
  28. p:suffix=".jsp" />
  29. </beans>

4. 控制器



当前台发送请求:
将person=zhangsan:666传递到后台,被StringToPersonConverter转换为Person对象;
跳转到界面:test.jsp






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值