zoukankan      html  css  js  c++  java
  • JAVA springmvc 转换器

    一、有时候springmvc给咱们提供的数据转换并不能转换所有类型比如说由字符串类型转换Date类型,这时候需要我们自定义转换器,帮助springmvc转换我们需要的类型。

    二、1)定义我们的转换器:

     1 package jd.com.contronller.jd.com.convert;
     2 
     3 import org.springframework.core.convert.converter.Converter;
     4 
     5 import java.text.ParseException;
     6 import java.text.SimpleDateFormat;
     7 import java.util.Date;
     8 
     9 public class CustomStringToDate implements Converter<String,Date> {
    10     @Override
    11     public Date convert(String s) {
    12         try {
    13             Date date= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(s);
    14             return date;
    15         } catch (ParseException e) {
    16             e.printStackTrace();
    17         }
    18         return null;
    19     }
    20 }

     2)在springmvc配置文件添加我们的转换器:

    1         <!--自定义转换器 注意在注解驱动里指明引用-->
    2     <bean id="conversionService"
    3     class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    4     <property name="converters">
    5     <set>
    6         <bean class="jd.com.contronller.jd.com.convert.CustomStringToDate"/>
    7     </set>
    8         </property>
    9         </bean>

     3)需要注意:在注解驱动的添加我们的转换器id

    1 <mvc:annotation-driven conversion-service="conversionService"  />

    因为转换器是处理器适配器进行处理的,如果我们不采用注解的方式,传统的引用如下:

    <?xmlversion="1.0"encoding="UTF-8"?>
    <beansxmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
            http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    
        <!-- 扫描带Controller注解的类 -->
        <context:component-scanbase-package="cn.itcast.springmvc.controller"/>
        
        <!-- 转换器配置 -->
        <beanid="conversionService"
            class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
            <propertyname="converters">
                <set>
                    <beanclass="jd.com.contronller.jd.com.convert.CustomStringToDate"/>
                </set>
            </property>
        </bean>
        <!-- 自定义webBinder -->
        <beanid="customBinder"    class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
            <propertyname="conversionService"ref="conversionService"/>
        </bean>
        <!--注解适配器 -->
        <beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
            <propertyname="webBindingInitializer"ref="customBinder"></property>
        </bean>
        <!-- 注解处理器映射器 -->
        <beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

     注意的:

    前端页面想显示日期格式需要定义下面标签:

    1  <td><fmt:formatDate value="${n.create_time}" pattern="yyyy-MM-dd HH:mm:ss" /> </td>

     需要注意格式需要和后面转换器类的格式一致,否则报错!!!!

  • 相关阅读:
    mouse_event模拟鼠标滚轮
    润乾报表配置技术路线
    建筑 物件 开心背单词 读句子,单词,字母,看图例, 翻译,看动画
    文字过渡动画,曲线过渡动画,,使用这个插件assign shape keys
    运动锻炼 开心背单词 读句子,单词,字母,看图例, 翻译,看动画,学英语,轻松背单词,简单背单词
    blender293 内置插件 精度绘画控件,PDT学习003,pdt tangents 切线
    日常用品 背单词 读句子 看图片 读单词 读字母 翻译, 看动画 学英语
    blender293 内置插件 精度绘画控件,PDT学习 precision drawing tools
    乔布斯 背单词 02 读句子 单词 字母 翻译,看动画 学英语 名言 我菜顾我在,我菜故我在,blender加python
    狐狸 和 乌鸦 英语 朗读句子 背单词
  • 原文地址:https://www.cnblogs.com/evilliu/p/8977165.html
Copyright © 2011-2022 走看看