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


相关推荐

  • 闭关看书

    闭关看书

    2021年8月6日
    54
  • Linux挂载磁盘分区「建议收藏」

    Linux挂载磁盘分区「建议收藏」Linux系统一般都会有未挂载的磁盘,如果我们想使用这些为挂载的磁盘就需要挂载到指定目录才能使用。一、有多个磁盘,将未分区的磁盘挂载1、进入root用户su–2、查看已挂载磁盘的使用情况:df–h可以看到系统已经挂载了sda磁盘,并分为sda1、sda2、sda3,3个分区3、查看所有磁盘信息(包括未挂载磁盘):fdisk–l4、创建新的磁盘…

    2022年6月19日
    49
  • 十进制转换为二进制java_二进制转八进制算法

    十进制转换为二进制java_二进制转八进制算法将十进制转换为二进制将二进制转换为十进制1.将十进制转换为二进制:思路:对十进制的数进行除2取余法:/***讲10进制转化为二进制*@paramde:待转换的十进制*@return:转换后的二进制(string)*/publicstaticStringDecimal2Binary(intde){

    2022年10月18日
    0
  • android开发之Intent.setFlags()_让Android点击通知栏信息后返回正在运行的程序

    在应用里使用了后台服务,并且在通知栏推送了消息,希望点击这个消息回到activity,结果总是存在好几个同样的activity,就算要返回的activity正在前台,点击消息后也会重新打开一个一样的activity,返回好几次才能退出,而不能像qq之类的点击通知栏消息回到之前存在的activity,如果存在就不再新建一个activity说的有点绕,如果是遇到此类问题的肯定能懂,没遇到过

    2022年3月10日
    36
  • MYCCL复合特征码定位器及其使用教程[通俗易懂]

    MYCCL复合特征码定位器及其使用教程[通俗易懂]复合特征码辅助定位工具MyCCLby:Tanknight——————————-  自从CCL问世以来,特征码修改已经成为了对付杀毒软件的常用手法,但是所谓魔高一尺,道高一丈杀毒软件开始使用多重复合特征码来对付特征码修改就是说只有你同时改掉程序所有的守护特征码此程序才不被杀。  所以本程序的作用是进行多重特征…

    2025年7月24日
    1
  • 【linux命令】 tree命令

    【linux命令】 tree命令文章目录Tree命令安装方法一,yum安装方法二,源码安装Tree命令安装方法一,yum安装命令:yuminstalltree方法二,源码安装1.下载安装包,地址:http://mama.indstate.edu/users/ice/tree/2.解压安装1)Linux环境(CentOS6.5)下安装a.解压tree-1.7.0.tgz文件,命令:tar-zxvftree-1.7.0.tgzb.进入解压目录中,命令:cdtree-1.7.0      c.安装文件,命令:

    2022年7月24日
    6

发表回复

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

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