解决: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类型,最终导致了这个错误。

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

发生这一错误的主要原因是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>

一.局部转换

@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) {
        //转换日期 注意这里的转化要和传进来的字符串的格式一直 如2015-9-9 就应该为yyyy-MM-dd
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));// CustomDateEditor为自定义日期编辑器
    }
}

二.全局转换

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;

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>
相关文章
|
2月前
|
JSON Java 数据格式
java调用服务报错415 Content type ‘application/octet-stream‘ not supported
java调用服务报错415 Content type ‘application/octet-stream‘ not supported
193 6
|
2月前
|
编解码 Java 开发者
Java String类的关键方法总结
以上总结了Java `String` 类最常见和重要功能性方法。每种操作都对应着日常编程任务,并且理解每种操作如何影响及处理 `Strings` 对于任何使用 Java 的开发者来说都至关重要。
313 5
|
5月前
|
Java
介绍String.format()方法中的格式占位符用法。
通过综合使用它们,可以在Java中构造非常具体和高度定制的输出格式。这对于输出报道、创建用户界面或者任何需要精确控制输出格式的场合都非常有用。记住,当使用格式化方法时,需要确保提供的输入参数与占位符类型匹配,否则会抛出 java.util.IllegalFormatException。
664 0
|
9月前
|
存储 数据库
【YashanDB知识库】YAS-00218 string conversion failed.
**简介:** 在数据导入导出过程中,使用 `imp` 工具导入 dump 文件时出现 `YAS-00218 string conversion failed` 错误。经分析,问题源于数据库字符集不匹配或原始 dump 文件中存在乱码。通过查看数据库服务端字符集(`show parameter character`)及采用折半查找法定位具体报错行,确认乱码导致导入失败。此问题影响版本为 23.2.1.100。
|
10月前
|
数据库
【YashanDB 知识库】exp 导出 csv 报错 YAS-00218 string conversion failed
**问题分类:** 数据导入导出 **关键字:** YAS-00218 string conversion failed **问题描述:** 使用 exp 导出成 CSV 时,报错 YAS-00218 string conversion failed。 **原因分析:** 数据库服务端字符集为 GBK,但客户端缺少 yasc_env.ini 文件,导致默认使用 UTF8 字符集。 **解决方法:** 创建 yasc_env.ini 文件并设置 CHARACTER_SET=GBK。 **影响范围:** 23.2.1.100
|
10月前
|
存储 数据库
【YashanDB 知识库】YAS-00218 string conversion failed.
**简介:** 在使用 `imp` 导入 dump 文件时,遇到错误 YAS-00218 string conversion failed。通过检查数据库字符集及使用折半查找法定位问题行,发现原始 dump 文件中存在乱码,导致导入失败。此问题影响版本 23.2.1.100。建议检查并修正源数据字符编码后再进行导入操作。
|
10月前
|
网络协议 Java Shell
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
678 7
|
Java
【编程基础知识】(讲解+示例实战)方法参数的传递机制(值传递及地址传递)以及String类的对象的不可变性
本文深入探讨了Java中方法参数的传递机制,包括值传递和引用传递的区别,以及String类对象的不可变性。通过详细讲解和示例代码,帮助读者理解参数传递的内部原理,并掌握在实际编程中正确处理参数传递的方法。关键词:Java, 方法参数传递, 值传递, 引用传递, String不可变性。
288 1
【编程基础知识】(讲解+示例实战)方法参数的传递机制(值传递及地址传递)以及String类的对象的不可变性
|
Java
Unable to obtain OffsetDateTime from TemporalAccessor: {},ISO resolved to 2024-11-26T20:55:26 of type java.time.format.Parsed
Unable to obtain OffsetDateTime from TemporalAccessor: {},ISO resolved to 2024-11-26T20:55:26 of type java.time.format.Parsed
410 0