实现方式以字符串转Date为例说明:

全局配置

第一种:实现 Converter 接口

  • 实现类: 
    public class StringToDateConveter implements Converter {

        private String formatPatten;
    
        public StringToDateConveter(String formatPatten){
    this.formatPatten=formatPatten;
    } @Override
    public Date convert(String s) {
    return DateUtil.string2Date(s,formatPatten);
    }
    }
  • mvc.xml配置

    <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
    
    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    <property name="converters">
    <set>
    <bean class="com.lannong.api.www.converter.StringToDateConveter">
    <constructor-arg name="formatPatten" value="yyyy-MM-dd"/>
    </bean>
    </set>
    </property>
    </bean>
  • 配置到handlerAdapter

       <!--使用 ConfigurableWebBindingInitializer 注册conversionService-->
    <bean id="webBindingInitializer" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
    <property name="conversionService" ref="conversionService"/>
    </bean> <!-- 注册ConfigurableWebBindingInitializer 到RequestMappingHandlerAdapter-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="webBindingInitializer" ref="webBindingInitializer"/>
    </bean>

第二种:实现Formatter接口,与第一种实现方式类似

  • 实现类

    public class MyDateFormater implements Formatter<Date> {
    
        @Override
    public Date parse(String s, Locale locale) throws ParseException {
    return DateTimeUtil.string2Date(s,"yyyy-MM-dd");
    } @Override
    public String print(Date date, Locale locale) {
    return null;
    }
    }
  • mvc.xml配置

    <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
    
    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    <property name="formatters">
    <set>
    <bean class="com.lannong.api.www.converter.MyDateFormater"/>
    </set>
    </property>
    </bean>

第三种:实现WebBindingInitializer接口

  • 实现类

    public class MyWebBindingInitializer implements WebBindingInitializer {
    
        @Override
    public void initBinder(WebDataBinder binder, WebRequest request) { binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
    @Override
    public void setAsText(String text) {
    setValue(DateTimeUtil.string2Date(text, "yyyy-MM-dd"));
    }
    });
    }
    }
  • mvc.xml配置

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="webBindingInitializer">
    <bean class="com.lannong.api.www.binder.MyWebBindingInitializer"/>
    </property>
    <!-- others config -->
    </bean>

局部配置

在Controller中添加转换方法并添加@InitBinder

  • 代码

    @InitBinder
    public void initBinder(WebDataBinder webDataBinder) throws Exception{
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
    simpleDateFormat.setLenient(false);
    webDataBinder.registerCustomEditor(Date.class , new CustomDateEditor(simpleDateFormat , true));
    } 或 @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request) {
    binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
    @Override
    public void setAsText(String text) {
    setValue(DateTimeUtil.string2Date(text, "yyyy-MM-dd"));
    }
    });
    }

两种方式都可以,作用域和该方法作用域一样

使用@DateTimeFormat(pattern = "yyyy-MM-dd")

注解可以加在属性上,也可以加在方法上,需要导入joda-time.jar。另外日期参数的格式需要和patten定义的一致,否则会报400错误

spring mvc 参数类型转换的更多相关文章

  1. Spring mvc参数类型转换

    1,需求 有时候我们接收到的参数为String类型的,但是我们需要将它们转化为其他类型的如:date类型,枚举类型等等,spring mvc为我们提供了这样的功能. 2,配置文件 在springmvc ...

  2. spring mvc: 参数方法名称解析器(用参数来解析控制器下的方法)MultiActionController/ParameterMethodNameResolver/ControllerClassNameHandlerMapping

    spring mvc: 参数方法名称解析器(用参数来解析控制器下的方法)MultiActionController/ParameterMethodNameResolver/ControllerClas ...

  3. spring mvc 参数

    Struts(表示层)+Spring(业务层)+Hibernate(持久层) Struts: Struts是一个表示层框架,主要作用是界面展示,接收请求,分发请求. 在MVC框架中,Struts属于V ...

  4. spring mvc参数绑定

    spring绑定参数的过程 从客户端请求key/value数据,经过参数绑定,将key/value数据绑定到controller方法的形参上.springmvc中,接收页面提交的数据是通过方法形参来接 ...

  5. Spring MVC参数封装传递

    在Spring MVC中,前端JSP页面可以传递  基本类型(int,String).实体类型.包装类型.数组类型.集合类型(List.map )等. 假如在传递的类型中有 Date类型的字段,需要在 ...

  6. spring mvc 参数绑定

    基础类型 原始类型:id必须要传,否则报错. @RequestMapping("/test") @ResponseBody public ResponseData test(int ...

  7. Spring MVC 参数必填项导致客户端报 HTTP 400 并且无法进入断点的问题

    1.问题 Spring MVC 在参数上设置了必填项,post 请求时报 HTTP 400 并且未进入断点,如将“年龄”设置为了必填项: @RequestParam( value="age& ...

  8. Spring MVC 参数的绑定方法

    在Spring MVC中,常见的应用场景就是给请求的Url绑定参数.本篇就介绍两种最最基本的绑定参数的方式: 基于@RequestParam 这种方法一般用于在URL后使用?添加参数,比如: @Req ...

  9. Spring MVC参数处理

    使用Servlet API作为参数 HttpServletRequest HttpServletResponse HttpSession 使用流作为参数 总结 Spring MVC通过分析处理处理方法 ...

随机推荐

  1. CentOS7.6使用Virt-manager创建虚拟机报错

    Virt-manager创建虚拟机遇到的一个问题解决 环境:centos7.6 系统内核:3.10.0-957.el7.x86_64 virsh version 根据库编译:libvirt 4.5.0 ...

  2. Markdwon入门2

    插入表情 这里是指广义的表情包,包括表情.物体.动物等. :+1: :smile: :s :scream: :kissing_heart: :yum: :cry: :blush: :frog: :co ...

  3. PAT1057 stack(分块思想)

    1057 Stack (30分)   Stack is one of the most fundamental data structures, which is based on the princ ...

  4. 05-Flutter移动电商实战-dio基础_引入和简单的Get请求

    这篇开始我们学习Dart第三方Http请求库dio,这是国人开源的一个项目,也是国内用的最广泛的Dart Http请求库. 1.dio介绍和引入 dio是一个强大的Dart Http请求库,支持Res ...

  5. SIGAI机器学习第二十四集 聚类算法1

    讲授聚类算法的基本概念,算法的分类,层次聚类,K均值算法,EM算法,DBSCAN算法,OPTICS算法,mean shift算法,谱聚类算法,实际应用. 大纲: 聚类问题简介聚类算法的分类层次聚类算法 ...

  6. HTTP协议(待写)

    先来了解了解 TCP/IP TCP/IP(Transmission Control Protocol / Internet Protocol)是计算机通讯必须遵守的规则,是不同的通信协议的大集合,其里 ...

  7. 7kyu kata

    https://www.codewars.com/kata/isograms/train/java CW 大神 solution: public class isogram { public stat ...

  8. Mongo 安装及基本操作

    一. 安装 Mongo文档: https://docs.mongodb.com/v3.6/administration/install-enterprise-linux/ Linux mongo的配置 ...

  9. SQL基础-过滤数据

    一.过滤数据 1.使用WHERE子句 过滤数据:关键字WHERE SELECT 字段列表 FROM 表名 WHERE 过滤条件; 过滤条件一般由要过滤的字段.操作符.限定值三部分组成: 如: SELE ...

  10. 微信小程序前端function封装

    funtion的封装 utils =>http.js var tips = { 1: "没有网络", 999: "无效的请求", 5000: " ...