zoukankan      html  css  js  c++  java
  • Springmvc配置时间日期转换

    1.局部日期转换

      

    @Controller
    public class ProductController{
        @RequestMapping(value="/test/springmvc.do")
        public String test(String name,Date birthday){
            System.out.println(name+birthday);
            return "";    
        }
        //局部性的转换
        @InitBinder
        public void initBinder(WebDataBinder binder, WebRequest request) {
            // TODO Auto-generated method stub
            //转换日期格式
             DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
             binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat,true));
        }
    }

    2.全局性的时间转换

    1).在springmvc.xml添加配置

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">    
        <!-- 日期格式转换 -->    
        <property name="webBindingInitializer">    
            <bean class="cn.itcast.core.web.DateConverter" />    
        </property>    
    </bean>

    2).编写 DateConverter

    public class DateConverter implements WebBindingInitializer {    
        
        public void initBinder(WebDataBinder binder, WebRequest request) {       
             DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
             binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat,true));   
        }
    }
  • 相关阅读:
    java学习笔记(三)
    JAVA 学习笔记(2)
    java学习笔记
    第二次作业完成情况
    第一次作业完成情况
    使用MarkDown标记语言发博客
    《Java高级程序设计》第一周作业
    纪逝去的毕业后的两年时光
    #这是来联系Markdown语法的
    CodeFirst初体验——问题三
  • 原文地址:https://www.cnblogs.com/menbo/p/9774066.html
Copyright © 2011-2022 走看看