首页 > 其他分享 >spring mvc环境之UI到控制器的自定义类型转换(十三)

spring mvc环境之UI到控制器的自定义类型转换(十三)

时间:2022-12-13 16:36:00浏览次数:48  
标签:类型转换 return 自定义 spring SimpleDateFormat source new

 

spring其实有默认的类型转换,比如前端表单提交数字的字符串,在控制器可接收为int或string都是没有错的.另外控制器也可以把前端数据接收为一个对象.

即使spring为我们考虑的很周全了,但是spring还是继承了他一贯的思想(自己永远都不是一个完美的人),总有考虑不周的地方....

扩展自定义的类型转换就是我们这篇要介绍的.

spring这种扩展一般的步骤都是,
1.继承某个接口,或他们已经把接口的实现类提供了,我们继承实现类后;
2.把自己写的实现类,以配置或注解的形式让spring容器管理.
3.在或是让spring容器管理后,再以某个bean的参数的形式注入到容器的某个模块中.

-------------

下来我们说说自定义类型转换的配置实现步骤:

1.写一个类TestConverter继承Converter可带泛型(Converter<S, T> ,S:代表源数据类型 T:代表目标数据类型);

2.在spring-mvc.xml中配置TestConverter让spring容器管理;

3.然后还要配置org.springframework.context.support.ConversionServiceFactoryBean让spring容器管理其名称,自己写的转换类作为参数注入其中;

4.ConversionServiceFactoryBean配置到mvc:annotation-driven中.

 

 

 

============================

1.演示一个时间转换器,

1).定义自己的类型转换器  继承一个父接口 Converter<S, T>

package com.cc8w.convertion;

import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;

public class MyDateConvertion  implements Converter<String, Date> {
    @Override
    public Date convert(String source) {
        /*SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");*/
        SimpleDateFormat sdf=getDateFormat(source);
        try {
            return sdf.parse(source);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    private SimpleDateFormat getDateFormat(String source) {
        //进行正则匹配
        //2016-12-14
        //2016/12/14
        //20161214
        if(Pattern.matches("^\\d{4}-\\d{2}-\\d{2}$", source)){
            return new SimpleDateFormat("yyyy-MM-dd");
        }else if(Pattern.matches("^\\d{4}/\\d{2}/\\d{2}$", source)){
            return new SimpleDateFormat("yyyy/MM/dd");
        }else if(Pattern.matches("^\\d{4}\\d{2}\\d{2}$", source)){
            return new SimpleDateFormat("yyyyMMdd");
        }else if(Pattern.matches("^\\d{4}年\\d{2}月\\d{2}日$", source)){
            return new SimpleDateFormat("yyyy年MM月dd日");
        }
        return null;
    }
    
}

2).在配置文件中注册自定义类型转化器,将该类交由spring容器管理

 

<!-- 01.注册自定义的类型转换器 -->
    <bean id="myDateConvertion" class="com.cc8w.convertion.MyDateConvertion"></bean>
    <!--02.注册类型转换器的服务工厂  产生转换对象  -->
    <bean id="convertionservice" class="org.springframework.context.support.ConversionServiceFactoryBean">
        <property name="converters">
            <list>
                <bean class="com.cc8w.convertion.MyDateConvertion" />
            </list>
        </property>
    </bean>
    <!--03.开启springMVC框架注解的支持,增加类型转换器,使其生效-->
    <mvc:annotation-driven conversion-service="convertionservice"/>

配置完成

------------------------------------------------

1.前端提交一个日期字符串(2022-02-02)控制器会自动用自定义的类型转换器

类型转换器验证
<form action="./index/index08" method="get"  >
    date:<input type="text" name="date" />
    <input type="submit" value="submit" />
</form>
    @RequestMapping("index08")
    public ModelAndView index08(Date date){
        System.out.println(date);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("index01");
        return mv;
    }

 

 

 

转 https://www.cnblogs.com/yejiaojiao/p/6163686.html

https://blog.csdn.net/weixin_57176230/article/details/124822977

 

标签:类型转换,return,自定义,spring,SimpleDateFormat,source,new
From: https://www.cnblogs.com/fps2tao/p/16979176.html

相关文章

  • SpringBoot集成Sentinel熔断处理
    SpringBoot服务配置1.引入依赖<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-senti......
  • Winform Vs Installer之添加自定义安装流程
    1、简介在Winform安装工具之VsInstaller介绍了VsInstaller的基本使用,可以满足基本需求,但是开发中遇到一些需要自定义安装流程的需求,如何通过VsInstaller来完成......
  • Cannot invoke “org.springframework.web.servlet.mvc.condition.PatternsRequestCon
    异常信息:Cannotinvoke“org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.getPatterns()”because“this.condition”isnull出现该异常......
  • SpringBoot和VUE
    一、案例结构用springboot做后端接口,采用restful风格。用vue-cli来创建前端项目,通过axios进行前后端交互。来实现用户的增删改查操作。二、效果图点击修改:点击添加:三、服务......
  • js中的强制类型转换、运算符、关系运算符、逻辑运算符、条件运算符
    1、强制类型转换Number1.1代码<!DOCTYPEhtml><html><head><metacharset="utf-8"><title>强制类型Number</title><styletype="text/css"></style>......
  • Spring Integration对Redis的支持
    SpringIntegration2.1引入了对Redis​的支持:“一个开源的高级键值存储”。这种支持以基于Redis以及发布-订阅消息传递适配器的形式出现,Redis通过其 PUBLISH、SUBSCRI......
  • Spring Boot Hello World
    在我使用熟悉的工具链中,好像​​vscode​​也能开发​​SpringBoot​​,但是我还是选择了专业的​​IDEA​​,因为穷,选择了社区版。之前在​​https://spring.io/​​根据......
  • 【Unity】 HTFramework框架(三十七)Inspector自定义序列化检视器
    更新日期:2020年8月21日。Github源码:​​​[点我获取源码]​​​Gitee源码:​​[点我获取源码]​​索引​​Inspector自定义序列化检视器​​​​使用​​​​Dropdown下拉......
  • SpringBoot整合RabbitMQ
    1、Maven依赖<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.0.RELEASE</vers......
  • Unity Editor 自定义属于你的DefaultHeaderGUI
    DefaultHeaderGUI默认页眉GUI,是Unity编辑器中的所有对象被选中后在Inspector界面显示的页眉GUI,如下图红框区域:在这个区域加点自己的东西。finishedDefaultHeaderGUI只需要......