解决: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>

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

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 几款主流好用的富文本编辑器(所见即所得常用编辑器)介绍

    几款主流好用的富文本编辑器(所见即所得常用编辑器)介绍富文本编辑器 富文本编辑器(RichTextEditor,RTE)是一种可内嵌于浏览器,所见即所得的文本编辑器。它提供类似于OfficeWord的编辑功能,方便那些不太懂HTML用户使用,富文本编辑器的应用非常广泛,它的历史与图文网页诞生的历史几乎一样长。 作为一个技术人员,手上备上两款富文本编辑器还是很有用的,指不定那个项目就要集成一个进去。到时候现找现用那可就费功夫了,毕竟从开…

    2022年6月10日
    365
  • 获取 Windows Phone 手机系统信息

    wpf:1161718192021222324252627282930…

    2021年12月20日
    48
  • 各个数据库中substring截取字符串操作

    各个数据库中substring截取字符串操作SQL中的substring函数是用来抓出一个栏位资料中的其中一部分。这个函数的名称在不同的资料库中不完全一样:MySQL:SUBSTR(),SUBSTRING()

    2022年5月23日
    55
  • 分布式通信协议RPC协议简介

    分布式通信协议RPC协议简介定义RPC(RemoteProcedureCallProtocol)远程过程调用协议,使得我们客户端在不知道调用细节的情况下去调用远程计算机的某个程序中的某个函数时,就跟调用本地函数一样。RPC协议其实是一个规范,其实现框架有Dubbo、Thrift、RMI、WebService、Hessain等。RPC框架的特点是网络协议和网络IO对于调用端和服务端是透明的。RPC框架会封装隐藏底层的通信细节和网络IO细节。服务调用方与服务提供方的调用流程:一个RPC框架应该具有的要素:RPC客户端:

    2022年5月19日
    42
  • Nginx搭建视频点播和视频直播服务器

    Nginx搭建视频点播和视频直播服务器Nginx搭建视频点播和视频直播服务器一·、环境:Centos7,(推荐,Ubuntu不是很好用,经常会有一些莫名其妙的报错)Nginx1.10.1二、系统环境搭建首先,我是不建议自己一个个去安装这些软件的,耗时耗力,而且,容易出错,所以,最好使用yuminstall***命令安装,出错的概率小。资源链接:链接:https://pan.baidu.com/s/1WmJYpQ_b…

    2022年6月14日
    32
  • 专家推荐的十种健康食物

    专家推荐的十种健康食物

    2021年7月23日
    52

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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