zoukankan      html  css  js  c++  java
  • Converter实现Date类型转换

    1.springmvc-config.xml配置

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           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.3.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-4.3.xsd
           http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        <context:annotation-config/>
        <context:component-scan base-package="com.wxy.controller"/>
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
        <mvc:annotation-driven conversion-service="conversionService" />
        <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
            <property name="converters">
                <set>
                    <bean class="com.wxy.convert.DateConverter" />
                </set>
            </property>
        </bean>
    </beans>

    2.日期转换类DateConverter.java

     1 package com.wxy.convert;
     2 
     3 import org.springframework.core.convert.converter.Converter;
     4 import java.text.ParseException;
     5 import java.text.SimpleDateFormat;
     6 import java.util.Date;
     7 
     8 public class DateConverter implements Converter<String,Date> {
     9     private String datePattern = "yyyy-MM-dd HH:mm:ss";
    10     @Override
    11     public Date convert(String source){
    12         SimpleDateFormat simpleDateFormat = new SimpleDateFormat(datePattern);
    13         try{
    14             return simpleDateFormat.parse(source);
    15         }catch (ParseException e){
    16             throw new IllegalArgumentException(
    17                     "无效的日期格式,请使用这种格式:"+datePattern
    18             );
    19         }
    20     }
    21 }

    3.日期控制器类DateController

    package com.wxy.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import java.util.Date;
    @Controller
    public class DateController {
        @RequestMapping("/customDate")
        public String CustomDate(Date date){
            System.out.println("date="+date);
            return "success";
        }
    }

    目前是不完善版本,只能控制台输出,等完善jsp页面,再修改。

  • 相关阅读:
    UI-iOS开发中@property的属性weak nonatomic strong readonly等介绍
    UIView之userInteractionEnabled属性介绍
    UI-target...action设计模式,手势识别器.UIimageview
    UI-事件处理
    IOS开发—事件处理,触摸事件,UITouch,UIEvent,响应者链条,手势识别
    UI-事件,触摸与响应者链(一)
    第47月第11天 iOS 文件下载Download,支持断点续传、后台下载、设置下载并发数
    第47月第10天 telnet rpm包安装
    第47月第5天 openresty
    第47月第4天 arkit录制
  • 原文地址:https://www.cnblogs.com/wxywxy/p/8728390.html
Copyright © 2011-2022 走看看