以下是一些需要类型转换的简单情况:
情况1。 为了帮助简化bean配置,Spring支持属性值与文本值之间的转换。 每个属性编辑器仅设计用于某些类型的属性。 为了使用它们,我们必须在Spring容器中注册它们。 案例2。 同样,在使用Spring MVC时,控制器会将表单字段值绑定到对象的属性。 假设对象是由另一个对象组成的,则MVC控制器无法自动将值分配给内部自定义类型对象,因为表单中的所有值都作为文本值输入。 Spring容器将文本值转换为原始类型,而不转换为自定义类型对象。 为此,我们必须在MVC流中初始化自定义编辑器。

本文将讨论实现自定义类型对象的转换器的各种方法。 为了详细说明这些,让我们考虑以下用例。 在该示例中,我想模拟游戏场地预订系统。 所以这是我的领域对象:

public class Reservation {public String playGround;public Date dateToReserve;public int hour;public Player captain;public SportType sportType;public Reservation(String playGround, Date date, int hour, Player captain, SportType sportType) {this.playGround = playGround;this.dateToReserve = date;this.hour = hour;this.captain = captain;this.sportType = sportType;}/*** Getters and Setters*/
}public class Player {private String name;private String phone;/*** Getters and Setters*/}public class SportType {public static final SportType TENNIS = new SportType(1, "Tennis");public static final SportType SOCCER = new SportType(2, "Soccer");private int id;private String name;public SportType(int id, String name) {this.id = id;this.name = name;}public static Iterable<SportType> list(){return Arrays.asList(TENNIS, SOCCER);}public static SportType getSport(int id){for(SportType sportType : list()){if(sportType.getId() == id){return sportType;}}return null;}/*** Getters and Setters*/
}

这是案例1的示例:假设我们要定义一个预留bean,这是我们的方法:

<bean id="dummyReservation" class="com.pramati.model.Reservation"><property name="playGround" value="Soccer Court #1"/><property name="dateToReserve" value="11-11-2011"/><property name="hour" value="15"/><property name="captain"><bean class="com.pramati.model.Player"><property name="name" value="Prasanth"/><property name="phone" value="92131233124"/></bean></property><property name="sportType"><property name="id" value="1"/><property name="name" value="TENNIS"/></property>
</bean>

这个bean的定义很冗长。 如果定义看起来像这样,它本来可以表现得更好:

<bean id="dummyReservation" class="com.pramati.model.Reservation"><property name="playGround" value="Soccer Court #1"/><property name="dateToReserve" value="11-11-2011"/><property name="hour" value="15"/><property name="captain" value="Prasanth,92131233124"/><property name="sportType" value="1,TENNIS"/>
</bean>

为此,我们应该告诉Spring在定义bean的过程中使用自定义转换器。

这是案例2的示例:假设我的应用程序中有一个JSP,它允许用户在一天的特定时间预订游乐场。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!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=ISO-8859-1">
<title>Reservation Form</title>
<style type="text/css">
.error {color: #ff0000;font-weight: bold;
}
</style>
</head>
<body><form:form method="post" modelAttribute="reservation"><table><tr><th>Court Name</th><td><form:input path="courtName" /></td></tr><tr><th>Reservation Date</th><td><form:input path="date" /></td></tr><tr><th>Hour</th><td><form:input path="hour" /></td></tr><tr><th>Player Name</th><td><form:input path="player.name" /></td></tr><tr><th>Player Contact Number</th><td><form:input path="player.phone" /></td></tr><tr><th>Sport Type</th><td><form:select path="sportType" items="${sportTypes}"itemLabel="name" itemValue="id" /></td></tr><tr><td colspan="3"><input type="submit" name="Submit" /></td></tr></table></form:form>
</body>
</html>

这是对应的MVC控制器:

@Controller
@RequestMapping
@SessionAttributes("reservation")
public class ReservationFormController {@Autowiredprivate ReservationService reservationService;@ModelAttribute("sportTypes")public Iterable<SportType> getSportTypes(){return SportType.list();}@RequestMapping(value="/reservationForm/{captainName}", method=RequestMethod.GET)public String initForm(Model model, @PathVariable String captainName){Reservation reservation = new Reservation();reservation.setPlayer(new Player(captainName, null));reservation.setSportType(SportType.TENNIS);model.addAttribute("reservation", reservation);return "reservationForm";}@RequestMapping(value="/reservationForm/{captainName}",method=RequestMethod.POST)public String reserve(@Valid Reservation reservation, BindingResult bindingResult, SessionStatus sessionStatus){validator.validate(reservation, bindingResult);if(bindingResult.hasErrors()){return "/reservationForm";} else{reservationService.make(reservation);sessionStatus.setComplete();return "redirect:../reservationSuccess";}}
}

现在您可以看到,在JSP中,我们将表单字段绑定到Reservation对象(modelAttribute =“ reservation”)。 该对象由传递给视图的控制器(在initForm()方法中)保留在模型中。 现在,当我们提交表单时,Spring会引发一条验证消息,指出字段值无法转换为类型Player和SportType。 为此,我们必须定义自定义转换器并将其注入Spring MVC流中。

现在的问题是如何定义自定义转换器? Spring提供了两种支持这些自定义转换器的方式:

  • 解决方案#1:使用PropertyEditors
  • 解决方案2:使用转换器

使用PropertyEditor:

PropertyEditorSupport,实现PropertyEditor接口,是用于帮助构建PropertyEditor的支持类。

public class SportTypeEditorSupport extends PropertyEditorSupport {/*** Sets the property value by parsing a given String.  May raise* java.lang.IllegalArgumentException if either the String is* badly formatted or if this kind of property can't be expressed* as text.** @param text  The string to be parsed.*/@Overridepublic void setAsText(String text) throws IllegalArgumentException {try{SportType sportType = SportType.getSport(Integer.parseInt(text));setValue(sportType);// setValue stores the custom type Object into a instance variable in PropertyEditorSupport.}catch(NumberFormatException nfe){throw new RuntimeException(nfe.getMessage());}}/*** Gets the property value as a string suitable for presentation* to a human to edit.** @return The property value as a string suitable for presentation*       to a human to edit.* <p>   Returns "null" is the value can't be expressed as a string.* <p>   If a non-null value is returned, then the PropertyEditor should*        be prepared to parse that string back in setAsText().*/@Overridepublic String getAsText() {SportType sportType = (SportType)getValue();return Integer.toString(sportType.getId());}
}

现在,在PropertyEditorRegistry中注册自定义编辑器。 PropertyEditorRegistrar在PropertyEditorRegistry中注册自定义编辑器。 这是您的操作方式:

import java.text.SimpleDateFormat;
import java.util.Date;import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.beans.propertyeditors.CustomDateEditor;import com.pramati.model.SportType;public class CustomPropertyEditorRegistrar implements PropertyEditorRegistrar {@Overridepublic void registerCustomEditors(PropertyEditorRegistry registry) {registry.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("dd-MM-yyyy"), true));registry.registerCustomEditor(SportType.class, new SportTypeEditorSupport());}
}

CustomEditorConfigurer被实现为Bean工厂后处理器,供您在实例化任何Bean之前注册自定义属性编辑器。 为此,我们将PropertyEditorRegistry与CustomEditorConfigurer关联。

<bean id="customPropertyEditorRegistrar" class="com.pramati.spring.mvc.CustomPropertyEditorRegistrar"/><bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"><property name="propertyEditorRegistrars"><list><ref bean="customPropertyEditorRegistrar"/></list></property>
</bean>

现在,当Spring容器看到​​此信息时:

<property name="captain" value="Prasanth,92131233124"/>

Spring自动将指定的值转换为Player对象。 但是,此配置对于Spring MVC流还不够。 控制器仍然会抱怨类型不兼容,因为它期望一个Player对象在获取String的地方。 为了将表单字段值解释为自定义类型对象,我们必须进行很少的MVC配置更改。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
import org.springframework.validation.Validator;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.context.request.WebRequest;public class CustomWebBindingInitializer implements WebBindingInitializer {@Autowiredprivate CustomPropertyEditorRegistrar customPropertyEditorRegistrar;@Overridepublic void initBinder(WebDataBinder binder, WebRequest request) {customPropertyEditorRegistrar.registerCustomEditors(binder);}
}

现在,根据需要将WebBindingInitializer注入RequestMappingHandlerAdapter中,手动删除并定义必要的bean。

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/><bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"><property name="webBindingInitializer"><bean class="com.pramati.spring.mvc.CustomWebBindingInitializer"/></property>
</bean>

现在,控制器会自动将String转换为必要的自定义类型对象。 请注意,我们必须进行单独的配置更改,以简化Spring MVC中的bean配置和表单字段的类型转换。 同样值得指出的是,在扩展PropertyEditorSupport时,我们将自定义类型对象存储在实例变量中,因此使用PropertyEditors并不是线程安全的。 为了克服这些问题,Spring 3.0引入了转换器和格式化程序的概念。

使用转换器:

转换器组件用于将一种类型转换为另一种类型,并通过强制将所有与转换相关的代码放在一个位置来提供更清晰的分隔。 Spring已经支持常用类型的内置转换器,并且该框架具有足够的可扩展性,可以编写自定义转换器。 Spring Formatters进入图片以根据渲染数据的格式格式化数据。 在考虑编写适合特定业务需求的自定义转换器之前,总是有必要先查看详尽的预建转换器列表。 有关查看预建转换器的列表,请参见org.springframework.core.convert.support软件包。

回到我们的用例,让我们实现String到SportType转换器:

import org.springframework.core.convert.ConversionFailedException;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.Converter;
import com.pramati.model.SportType;public class StringToSportTypeConverter implements Converter<String, SportType> {@Overridepublic SportType convert(String sportIdStr) {int sportId = -1;try{sportId = Integer.parseInt(sportIdStr);} catch (NumberFormatException e) {throw new ConversionFailedException(TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(SportType.class), sportIdStr, null);}SportType sportType = SportType.getSport(sportId);return sportType;}}

现在,在ConversionService中注册它,并将其与SpringMVC流链接:

<mvc:annotation-driven conversion-service="conversionService"/><bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean" ><property name="converters"><set><bean class="com.pramati.type.converters.StringToSportTypeConverter"/><bean class="com.pramati.type.converters.StringToDateConverter"/><bean class="com.pramati.type.converters.StringToPlayerConverter"/></set></property>
</bean>

如果使用的是自定义bean声明而不是‹mvc:annotation-driven /›,则可以使用以下方法:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
import org.springframework.validation.Validator;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.context.request.WebRequest;public class CustomWebBindingInitializer implements WebBindingInitializer {@Autowiredprivate ConversionService conversionService;@Overridepublic void initBinder(WebDataBinder binder, WebRequest request) {binder.setConversionService(conversionService);}}

现在将WebBindingInitializer注入RequestMappingHandlerAdapter中。

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"><property name="webBindingInitializer"><bean class="com.pramati.spring.mvc.CustomWebBindingInitializer"/></property>
</bean>

单独注册ConversionService将有助于简化bean配置(案例1)。 为了使案例2正常工作,我们必须在Spring MVC流中注册ConversionService。 请注意,这种类型转换方式也是线程安全的。

同样,除了使Converters / PropertEditor对应用程序中的所有控制器都可用之外,我们还可以基于每个控制器启用它们。 这是您的操作方式。 删除上面指定的通用配置,并在控制器类中引入@InitBinder,如下所示:

@Controller
@RequestMapping
@SessionAttributes("reservation")
public class ReservationFormController {private ReservationService reservationService;private ReservationValidator validator;@Autowiredpublic ReservationFormController(ReservationService reservationService, ReservationValidator validator){this.reservationService = reservationService;this.validator = validator;}@Autowiredprivate ConversionService conversionService;@InitBinderprotected void initBinder(ServletRequestDataBinder binder) {binder.setConversionService(conversionService);}/*@InitBinderprotected void initBinder(ServletRequestDataBinder binder) {binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("dd-MM-yyyy"), true));binder.registerCustomEditor(SportType.class, new SportTypeEditorSupport(reservationService));}*//*@Autowiredprivate PropertyEditorRegistrar propertyEditorRegistrar;@InitBinderprotected void initBinder(ServletRequestDataBinder binder) {propertyEditorRegistrar.registerCustomEditors(binder);}*/@ModelAttribute("sportTypes")public Iterable<SportType> getSportTypes(){return SportType.list();}@RequestMapping(value="/reservationForm/{userName}", method=RequestMethod.GET)public String initForm(Model model, @PathVariable String userName){Reservation reservation = new Reservation();reservation.setPlayer(new Player(userName, null));reservation.setSportType(SportType.TENNIS);model.addAttribute("reservation", reservation);return "reservationForm";}@RequestMapping(value="/reservationForm/{userName}",method=RequestMethod.POST)public String reserve(@Valid Reservation reservation, BindingResult bindingResult, SessionStatus sessionStatus){validator.validate(reservation, bindingResult);if(bindingResult.hasErrors()){return "/reservationForm";} else{reservationService.make(reservation);sessionStatus.setComplete();return "redirect:../reservationSuccess";}}@RequestMapping("/reservationSuccess")public void success(){}
}

因此,如果您看到上面的代码,您会注意到在使用PropertyEditor而不是转换器的地方,注释的代码。 因此,在两种实现方式中都可以使用基于控制器启用类型转换器的功能。

参考: prasanthnath博客上的JCG合作伙伴 Prasanth Gullapalli 在Spring中进行了类型转换 。

翻译自: https://www.javacodegeeks.com/2013/11/type-conversion-in-spring-2.html

Spring中的类型转换相关推荐

  1. spring 类型转换器_Spring中的类型转换

    spring 类型转换器 以下是一些需要类型转换的简单情况: 情况1. 为了帮助简化bean配置,Spring支持属性值与文本值之间的转换. 每个属性编辑器仅设计用于某些类型的属性. 为了使用它们,我 ...

  2. spring中这些能升华代码的技巧,可能会让你爱不释手

    前言 最近越来越多的读者认可我的文章,还是件挺让人高兴的事情.有些读者私信我说希望后面多分享spring方面的文章,这样能够在实际工作中派上用场.正好我对spring源码有过一定的研究,并结合我这几年 ...

  3. (转)Spring中ThreadLocal的认识

    我们知道Spring通过各种DAO模板类降低了开发者使用各种数据持久技术的难度.这些模板类都是线程安全的,也就是说,多个DAO可以复用同一个模板实例而不会发生冲突.我们使用模板类访问底层数据,根据持久 ...

  4. (转)Spring中Singleton模式的线程安全

    不知道哪里的文章,总结性还是比较好的.但是代码凌乱,有的还没有图.如果找到原文了可以进行替换! spring中的单例 spring中管理的bean实例默认情况下是单例的[sigleton类型],就还有 ...

  5. spring 中单利模式的理解

    一.Spring单例模式与线程安全 Spring框架里的bean,或者说组件,获取实例的时候都是默认的单例模式,这是在多线程开发的时候要尤其注意的地方. 单例模式的意思就是只有一个实例.单例模式确保某 ...

  6. 详解Spring中@Autowire,@Value 注解实现原理

    点击关注公众号,实用技术文章及时了解 来源:blog.csdn.net/u013202238/article/details/107879726 本文主要基于SpringBoot-2.3.3.RELE ...

  7. Spring 中那些让你爱不释手的代码技巧

    前言 最近越来越多的读者认可我的文章,还是件挺让人高兴的事情.有些读者私信我说希望后面多分享spring方面的文章,这样能够在实际工作中派上用场.正好我对spring源码有过一定的研究,并结合我这几年 ...

  8. 基于佟刚老师视频教程的Spring MVC数据类型转换学习总结

    基于Spring MVC数据类型转换的学习笔记总结 问题 页面中数据格式为字符串格式,后台pojo中对应的类型可能为另外一种对象. 如: 在前台页面输入一个固定格式的时间字符串,在后台接收为一个jav ...

  9. Spring中Singleton模式的线程安全

    不知道哪里的文章,总结性还是比较好的.但是代码凌乱,有的还没有图.如果找到原文了可以进行替换! spring中的单例 spring中管理的bean实例默认情况下是单例的[sigleton类型],就还有 ...

最新文章

  1. 让Centos5.6的Firefox支持Java
  2. 《Spring Cloud与Docker微服务架构实战》配套代码
  3. 双目立体匹配经典算法之Semi-Global Matching(SGM)概述:匹配代价计算之互信息(Mutual Information,MI)...
  4. 总结PHP如何获取当前主机、域名、网址、路径、端口和参数等
  5. springboot~读取自定义配置项
  6. android 5.1禁止休眠
  7. seaweedfs问题处理
  8. 小米手机的专用计算机连接软件,详细教您小米手机怎么连接电脑
  9. android获取手机短信记录,Android开发获取短信的内容并截取短信
  10. 驱动器控制模式功能简介(CSP,CSV,CST等)
  11. 韩寒首度回应小三事件:望女友妻子和平相处_陕西频道_凤凰网
  12. 遥感影像语义分割论文笔记(一)FarSeg CVPR2020
  13. js + css 做出网易云音乐的轮播图
  14. 百度云链接失效问题与解决
  15. 超级玛丽马里奥版下载_将超级马里奥赋予生命
  16. webcam实现拍照
  17. 青岛科技大学计算机田玉平,控制理论与控制工程;就业怎么样/?博士.
  18. 微信投票系统java开发_微信投票系统 v3.0
  19. Linux 内核开发者完成了对所有来自 UMN.edu 补丁的审查
  20. 嵩天-Python语言程序设计程序题--第五周:函数和代码复用

热门文章

  1. 2-计算机发展及应用
  2. 转:centos8开启防火墙端口
  3. JDBC元数据操作(一)-- DatabaseMetaData接口详解
  4. checkpoint_通过Main的Checkpoint Restore加快Java启动速度
  5. 开发罪过_七大罪过与如何避免
  6. zxing qr区域判断_如何在Java中使用Zxing和JFreeSVG创建QR Code SVG?
  7. 网络研讨室_即将举行的网络研讨会:调试生产中Java的5种最佳实践
  8. php cdi_Swing应用程序中的CDI事件将UI和事件处理分离
  9. javabeans_(单元测试)JavaBeans的技巧
  10. 成为Java流大师–第6部分:使用流创建新的数据库应用程序