zoukankan      html  css  js  c++  java
  • Spring mvc 3 视图页面传日期到controller层

    在Spring3 mvc中从前台到后台传递数据中如果包括日期类型的话,一般会报错: org.springframework.validation.BindException

    解决方法:

    1、新建java日期转换类DateConverter

    package com.sunney.commons; 
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import org.springframework.beans.propertyeditors.CustomDateEditor;
    import org.springframework.web.bind.WebDataBinder;
    import org.springframework.web.bind.support.WebBindingInitializer;
    import org.springframework.web.context.request.WebRequest;
    
    /**
     * Description:
     * spring3 mvc 的日期传递[前台-后台]bug: 
     * org.springframework.validation.BindException 
     * 的解决方式.包括xml的配置 
     * @author  LiChunming
     * @version V1.0 
     * @createDateTime:2013-2-1 下午02:04:04 
     * @Company: MSD. 
     * @Copyright: Copyright (c) 2011
     **/
    public class DateConverter implements WebBindingInitializer {  
      
        @Override  
        public void initBinder(WebDataBinder binder, WebRequest request) {  
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");  
            binder.registerCustomEditor(Date.class, new CustomDateEditor(df,false));  
        }  
    }  
    

    2、applicationContext.xml 配置

    1 <context:component-scan base-package="com.sunney" />
    2     <!-- 日期转换  必须放在<mvc:annotation-driven />前面 -->  
    3     <bean  class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
    4         <!-- 日期格式转换 -->  
    5         <property name="webBindingInitializer">  
    6             <bean class="com.sunney.commons.DateConverter" />  
    7         </property>
    8     </bean> 
    9     <mvc:annotation-driven />  

    3、applicationContext.xml 还得新增配置

    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
                            http://www.springframework.org/schema/mvc 
                            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"

     否则会报:The prefix "mvc" for element "mvc:annotation-driven" is not bound

     

  • 相关阅读:
    投票系统完善
    投票系统设计与实现
    一天天进步
    洛谷P4168 [Violet]蒲公英 题解 数列分块
    LOJ6285. 数列分块入门 9 题解
    洛谷P5340 大中锋的游乐场 题解 分层图最短路
    P1073 [NOIP2009 提高组] 最优贸易 题解 分层图最短路
    洛谷P7297 [USACO21JAN] Telephone G 题解 分层图最短路
    洛谷P1119 灾后重建 题解 Floyd算法
    安装redis 后本地系统空间越来越小
  • 原文地址:https://www.cnblogs.com/yuanermen/p/2888964.html
Copyright © 2011-2022 走看看