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

相关推荐

  • C++按位异或运算符

    C++按位异或运算符参与运算的两个值,如果两个相应位相同,则结果为0,否则为1。即:0^0=0,1^0=1,0^1=1,1^1=0例如:10100001^00010001=101100000^0=0,0^1=10异或任何数=任何数1^0=1,1^1=01异或任何数-任何数取反 任何数异或自己=把自己置0(1)按位异或可以用来使某些特定的位翻转,如对数10100001的第2位和

    2022年5月20日
  • contextmenu是什么事件_onpropertychange事件

    contextmenu是什么事件_onpropertychange事件用户点击鼠标右键时触发并打开上下文菜单禁用:document.oncontextmenu=function(){   returnfalse;}编辑自定义右键打开菜单document.oncontextmenu=function(){   returnfalse; } document.body.addEventListener(‘mousedown’…

    2022年8月14日
  • java中的Cipher类

    java中的Cipher类随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)该类位于javax.crypto包下,声明为publicclassCipherextendsObject此类为加密和解密提供密码功能。它构成了JavaCryptographicExtension…

    2022年9月4日
  • 2022电力电缆复训题库及在线模拟考试

    2022电力电缆复训题库及在线模拟考试题库来源:安全生产模拟考试一点通公众号小程序2022年电力电缆操作证考试题为电力电缆复习题考前押题!2022电力电缆复训题库及在线模拟考试根据电力电缆考试教材。电力电缆考试题库通过安全生产模拟考试一点通准确把握考试要点。1、【单选题】6~35kV三芯交联聚乙烯电缆共用()。(A)A、内护套B、中性导体C、地线2、【单选题】中性点经电阻接地的优点是,()。(A)A、减小了单相接地电流B、不需要增加设备C、加大了电磁干扰3、【单选题】传统的配电网,特别是农村电网多为()。(

    2022年5月7日
  • [面试分享]想跳槽涨薪,想进大厂,如何准备面试呢?

    [面试分享]想跳槽涨薪,想进大厂,如何准备面试呢?

    2022年2月19日
  • linux phpstorm激活码2021【在线注册码/序列号/破解码】[通俗易懂]

    linux phpstorm激活码2021【在线注册码/序列号/破解码】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月18日

发表回复

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

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