• 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页面,再修改。

  • 相关阅读:
    最简单的jQuery插件
    SQL执行时间
    Resharper 8.2 注册码
    Module模式
    RestSharp使用
    使用MVC过滤器保存操作日志
    Ajax Post 类实例
    IBatis分页显示
    IBatis插入类的实例
    Topcoder SRM629 DIV2 解题报告
  • 原文地址:https://www.cnblogs.com/wxywxy/p/8728390.html
Copyright © 2020-2023  润新知