zoukankan      html  css  js  c++  java
  • SpringMVC 处理Date类型数据@InitBinder @DateTimeFormat 注解 的使用

    使用SpringMVC的时候,需要将表单中的日期字符串转换成对应JavaBean的Date类型,而SpringMVC默认不支持这个格式的转换,解决方法有两种,如下:


    方法一 . 在需要日期转换的Controller中使用SpringMVC的注解@initbinder和Spring自带的WebDateBinder类来操作。

               /* 以下资料来自网络 */

               用@InitBinder注解的控制器方法,允许你直接在你的控制器类中配置 Web 数据绑定。@InitBinder标记初始化WebDataBinder的方法,WebDataBinder被用于填充被注解的处理方法的命令和表单对象参数。

         这些初始化绑定器(Init-binder)方法支持@RequestMapping方法支持的所有参数,处理命令/表单对象以及相关的校验结果对象。初始化绑定器方法必须不带返回值,所以它们通常被声明为 void 的。典型的参数包括WebDataBinderWebRequest或 者java.util.Locale,允许用代码方式注册特定上下文的编辑器(context-specific editors)。

               /* 以上资料来自网络 */
           WebDataBinder是用来绑定请求参数到指定的属性编辑器.由于前台传到controller里的值是String类型的,当往Model里Set的数据是日期类型, Spring就会去找到对应的editor进行转换,将String类型转换成日期类型,然后再set进去.

          我们要进行类型转换,需要在springMvc里配置配型转换器,springMvc提供了很多类型转化器,配置方法如下:

        <!-- 日期转换 -->
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
            <property name="messageConverters">
                <list>
    <!--配置String类型的消息转换器-->
    <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html; charset=utf-8</value> </list> </property> </bean>
    <!--此处还可以定义其他类型的消息转换器 -->
    </list> </property> </bean>

       然后在需要进行类型转换的controller里创建初始化绑定器方法,用对应的编辑器将接受到的字符串转为日期,代码如下:

    @Controller
    public class MyFormController 
    {
        @InitBinder
        protected void initBinder(WebDataBinder binder) 
        {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            dateFormat.setLenient(false);
            binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
        }
    }

    spring mvc在绑定数据之前,都会先注册这些编辑器,Spring提供了很多编辑器的实现类,像CustomDateEditor ,CustomBooleanEditor,CustomNumberEditor等,使用时候调用WebDataBinder的registerCustomEditor方法.

     需要注意的是,此初始化绑定的方法,只在该Controller内生效.

    这样,我们前台传递的字符串日期信息,在后台javaBean中使用Date就可以直接接收到了.

    方法二: 使用@DateTimeFormat 注解方式,建议使用,此方式简单,且代码量少

         首先,@DateTimeFormat 注解使用到了joda包中的东西, 我们需要引入joda相关jar包.如果不引入会报异常

     <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.9.7</version>
    </dependency>

       然后,我们要在javaBean 中要接收的Date类型数据上加上注解: 如下:

    public class Person{
    
        private String name;
    
        private Integer age;
    
        private Integer sex; 
    
        //在date类型上加入注解,同时指定接收到的日期格式样式
        @DateTimeFormat(pattern="yyyy-MM-dd")
        private Date birthday;
    }

     最后,在SpringMVC配置XML文件中进行注解驱动的配置:

        <!-- 日期转换 -->
        <mvc:annotation-driven conversion-service="conversionService" />
        <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"></bean>
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
            <property name="messageConverters">
                <list>
                    <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                        <property name="supportedMediaTypes">
                            <list>
                                <value>text/html; charset=utf-8</value>
                            </list>
                        </property>
                    </bean>
                </list>
            </property>
        </bean>

    这样就完成了,后续只要的新增的javaBean中给日期类型加上@DateTimeFormat注解即可,任意contorller中使用javaBean接受数据时,传递的String类型数据会自动转换成对应日期类型

    需要注意的是 接收字符串的日期格式需要与@DateTimeFormat注解后定义的格式一致.

         

  • 相关阅读:
    QT之QRect函数QRect::adjust()函数
    QT 正则表达式(进阶篇)IP,端口号,文件名,非空格字符的匹配,已验证
    QT 正则表达式(基础篇)
    处理不平衡数据的策略
    记录一下ssh,nfs安装步骤
    用户偏好的回归预测推荐
    SVD++分解
    BiasLFM分解
    WALS分解
    ALS分解
  • 原文地址:https://www.cnblogs.com/huaixiaonian/p/8649683.html
Copyright © 2011-2022 走看看