解决: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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • java 图书馆管理系统(面向接口编程)

    java 图书馆管理系统(面向接口编程)图书馆管理系统功能要求:1.图书馆(对书本的管理)  1.初始化图书馆的书本  2.图书馆有提供借书给用户的功能  3.图书馆有提供用户还书到图书馆的功能  4.图书馆提供查询图书馆的所有图书的功能2.用户系统(对用户管理)  1.初始化用户  2.用户管理提供注册用户的功能(用户名:首字母小写,至少6位。密码:必须包含小写字母,大写字

    2022年7月7日
    35
  • __setattr__,setattr(),getattr__,getattr()及__getattribute__的区别

    __setattr__,setattr(),getattr__,getattr()及__getattribute__的区别Python2.7IDEPycharm5.0.3首先,给一波定义__setattr__(self,name,value)#当试图给特性name赋值时候自动被调用,其余方法同理#相当于触发机制__getattribute__(self,item)#每次通过实例化访问属性都会通过该函数#如果没有该属性,则访问完该函数后,再会通过__getattr__函数_setattr_和_get

    2025年6月23日
    4
  • 嵌入式Linux–menuconfig详解

    嵌入式Linux–menuconfig详解menuconfig工作原理menuconfig是一套图像化配置工具,由ncurses库提供软件支持。ncurses库提供了一系列的函数以便使用者调用它们去生成基于文本的用户界面。menuconfig本身的软件只负责提供menuconfig工作的这一套逻辑,比如说通过上下左右调整光标,Enter选中等,并不负责提供内容。menuconfig运行之后会读取Kconfig、读取/写入….

    2022年6月11日
    29
  • eclipse卸载android环境插件

    卸载eclipse-preference左侧导航栏中Android模块help-installnewsoftware–whatisalreadyinstalled?在弹出的界面选择android相关的插件卸载,重启eclipse

    2022年4月6日
    41
  • PhpStorm激活码2025.1.0.1版本最新教程,永久有效激活码,亲测可用,记得收藏

    PhpStorm激活码教程永久有效2025.1.0.1激活码教程-Windows版永久激活-持续更新,Idea激活码2025.1.0.1成功激活

    2025年5月23日
    5
  • git clone 出错

    git clone 出错在linux环境下,(我用的是虚拟机virtualbox,ubuntu16.04)以下错误是已经在远程仓库添加了对应的SSH的情况下出现的:错误1、出现fatal:Theremoteendhungupunexpectedlyfatal:过早的文件结束符(EOF)fatal:index-packfailedgitconfighttp.postBuffer52428…

    2022年7月21日
    19

发表回复

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

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