解决:Failed to convert value of type ‘java.lang.String‘ to required type ‘java.util.Date‘;

解决:Failed to convert value of type ‘java.lang.String‘ to required type ‘java.util.Date‘;发生这一错误的主要原因是Controller类中需要接收的是Date类型,但是在页面端传过来的是String类型,最终导致了这个错误。这里提供两种解决方案,一种是局部转换,一种是全局转换。一.局部转换@ControllerpublicclassUserController{ @RequestMapping(value=”/login.do”) publicStr

大家好,又见面了,我是你们的朋友全栈君。

关注微信公众号“假装正经的程序员”,回复“日期转换”即可获取解决方案

发生这一错误的主要原因是Controller类中需要接收的是Date类型,但是在页面端传过来的是String类型,最终导致了这个错误。

这里提供两种解决方案,一种是局部转换,一种是全局转换。

<form action="login.do" method="post">
		<input type="text" name="birthday" value="2017-07-12 22:04:00">
		<input type="submit" value="提交">
	</form>

解决:Failed to convert value of type ‘java.lang.String‘ to required type ‘java.util.Date‘;

一.局部转换

@Controller
public class UserController{
	
	@RequestMapping(value="/login.do")
	public String login(String username,Date birthday){
		System.out.println("________");
		return "";
	}

        //只需要加上下面这段即可,注意不能忘记注解
	@InitBinder
	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));// CustomDateEditor为自定义日期编辑器
	}
}

解决:Failed to convert value of type ‘java.lang.String‘ to required type ‘java.util.Date‘;

二.全局转换

1.创建CustomDate类实现WebBindingInitializer

import java.text.DateFormat;
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;

/**
 * @作者:JackHisen(GWD)
 * @项目名:shoppingmall
 * @时间:2017-7-12 下午10:39:10
 * @version 1.0
 */
public class CustomDate implements WebBindingInitializer{

	@Override
	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.在Spring-MVC.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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
	<context:component-scan base-package="com.gwd.shopping" use-default-filters="false">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	
	<!-- 日期转换 -->
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="webBindingInitializer">
			<bean class="com.gwd.shopping.core.web.CustomDate"/>
		</property>
	</bean>
	
	<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/back_page/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
</beans>

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/158425.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • 搭建备份服务器

    搭建备份服务器一、rsync的特性rsync的特性:1、支持拷贝特殊文件,设备等2、可以有排除指定文件或者目录同步的功能,相当于tar的排除功能3、可以做到保持源文件或目录的权限、时间、软硬连接、属主

  • title的中文_上什么下仿

    title的中文_上什么下仿VeryCD主页上的标题上总有一句名言,且是每次刷新变换的,看了一下代码,是用Math.floor(Math.random()*n.length);来实现的,verycd代码中源引的文件是:http://www.verycd.com/statics/title.saying但我所使用的是Windowsserver主机,所以要引用的是js文件,我的代码在这里:http://www.lidec

  • UpdatePanel 控件

    UpdatePanel 控件UpdatePanel控件使用了UpdatePanel控件的方案是ASP.NETAJAX扩展中的重要方案。我们收到了许多关于此控件、UpdateProgress控件以及二者功能的客户反馈。我们已经通过大量更改改善了部分页面呈现,并支持构建与UpdatePanel控件兼容的控件。我们还针对异步回发生命周期实现了丰富的事件模型,使您能够自定义客户端的更新处理。Sc

  • MSFCONSOLE COMMANDS「建议收藏」

    MSFCONSOLE COMMANDS「建议收藏」MSFCONSOLECORECOMMANDSTUTORIALTheMSFconsolehasmanydifferentcommandoptionstochoosefrom.ThefollowingareacoresetofMetasploitcommandswithreferencetotheiroutput.back…

  • linux ettercap,CentOS下安装ettercap

    linux ettercap,CentOS下安装ettercapCentOS下安装ettercapettercap下载地址http://ettercap.sourceforge.net/download.php下载好之后解压出来,进入到解压目录#tarzxvfettercap-NG-0.7.3.tar.gz#cdettercap-NG-0.7.3阅读README#vimREADMEMANDATORY://安装必须依赖的libpcap>=…

  • java创建线程池的几种方式_定时任务 java

    java创建线程池的几种方式_定时任务 java有时候有些需求不需要顺序执行,所以我就使用了多线程并行执行。废话不多说,上代码。1.创建线程池packageorg.java.multithreading;importorg.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;importorg.springframework.context.annotation.Configuration;importorg.springframework.scheduling.

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号