在本文中,您将会了解到关于Cannotconvertvalueoftype[java.lang.String]torequiredtype[java.util.Date]forproperty..
在本文中,您将会了解到关于Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property ...的新资讯,并给出一些关于C++之error: cannot bind non-const lvalue reference of type ‘myString&’ to an rvalue of type ‘m...、Can not deserialize value of type java.util.Date from String 异常解决办法、cannot convert t (type interface {}) to type string: need type assertion、cannot convert value of type 'String!' to expected argument type 'inout String'的实用技巧。
本文目录一览:- Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property ...
- C++之error: cannot bind non-const lvalue reference of type ‘myString&’ to an rvalue of type ‘m...
- Can not deserialize value of type java.util.Date from String 异常解决办法
- cannot convert t (type interface {}) to type string: need type assertion
- cannot convert value of type 'String!' to expected argument type 'inout String'
Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property ...
异常信息:
04-Aug-2014 15:49:27.894 SEVERE [http-apr-8080-exec-5] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [cms] in context with path [/cms] threw exception [Request processing failed; nested exception is org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 6 errors Field error in object ''carContractSearchBox'' on field ''createEndDate'': rejected value []; codes [typeMismatch.carContractSearchBox.createEndDate,typeMismatch.createEndDate,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [carContractSearchBox.createEndDate,createEndDate]; arguments []; default message [createEndDate]]; default message [Failed to convert property value of type ''java.lang.String'' to required type ''java.util.Date'' for property ''createEndDate''; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property ''createEndDate'': no matching editors or conversion strategy found]
先说明一下,我们的项目使用的是 Spring MVC。相应的功能是一个简单的 form 表单查询功能,里面有一些日期字段的查询。
相应的解决办法为:
在对应的 controller 中增加属性编辑器:
@InitBinder
protected void initBinder(WebDataBinder binder) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); }
注意这块的 new CustomDateEditor(dateFormat, true)
中的 true,查看 CustomDateEditor
源码可以看到:
/**
* Create a new CustomDateEditor instance, using the given DateFormat
* for parsing and rendering.
* <p>The "allowEmpty" parameter states if an empty String should
* be allowed for parsing, i.e. get interpreted as null value.
* Otherwise, an IllegalArgumentException gets thrown in that case.
* @param dateFormat DateFormat to use for parsing and rendering
* @param allowEmpty if empty strings should be allowed
*/
public CustomDateEditor(DateFormat dateFormat, boolean allowEmpty) { this.dateFormat = dateFormat; this.allowEmpty = allowEmpty; this.exactDateLength = -1; }
当 allowEmpty
字段为 true 的时候 form 表单传递的值可以为空。否则会出现 ""
字符串解析为 date 报错。
C++之error: cannot bind non-const lvalue reference of type ‘myString&’ to an rvalue of type ‘m...
先看代码(不想看代码可以直接看代码后的问题描述)
//header.h
#ifndef _HEADER_H
#define _HEADER_H
#define defaultSize 128
#include<iostream>
#include<string.h>
using namespace std;
class myString
{
private:
char *ch;
int curLength;
int maxSize;
public:
myString(int sz=defaultSize);
myString(const char *init);
myString(const myString& ob);
~myString(){delete []ch;}
void print();
int Length()const;
myString operator()(int pos, int len);
myString& operator = (myString& ob);
};
myString& myString::operator = (myString& ob)
{
if(&ob!=this)
{
delete[]ch;
this->ch = new char[ob.maxSize];
this->maxSize = ob.curLength;
strcpy(this->ch, ob.ch);
this->curLength = ob.curLength;
}
else
{
cerr<<"String copy error\n";
}
return *this;
}
myString myString::operator()(int pos, int len)
{
myString temp;
if(pos<0 || len<=0 || pos+len-1>=this->maxSize)
{
temp.curLength = 0;
temp.ch[0] = ''\0'';
}
else
{
if(pos+len-1 >= this->curLength)
len = this->curLength-pos;
temp.curLength = len;
for(int i=0,j=pos; i<len; ++i,++j)
temp.ch[i] = this->ch[j];
temp.ch[len] = ''\0'';
}
return temp;
}
int myString::Length()const
{
return this->curLength;
}
void myString::print()
{
cout<<this->ch<<endl;
}
myString::myString(int sz)
{
this->maxSize = sz;
this->ch = new char[this->maxSize+1];
if(this->ch == NULL)
{
cerr<<"Allocation ERROR\n";
exit(1);
}
this->curLength = 0;
ch[0] = ''\0'';
}
myString::myString(const char *init)
{
int len = strlen(init);
this->maxSize = (len > defaultSize) ? len : defaultSize;
this->ch = new char[this->maxSize+1];
if(this->ch == NULL)
{
cerr<<"Application Memory ERROR\n";
exit(1);
}
this->curLength = len;
strcpy(this->ch, init);
}
myString::myString(const myString& ob)
{
this->maxSize = ob.maxSize;
this->ch = new char[this->maxSize+1];
if(this->ch == NULL)
{
cerr<<"Application Memory ERROR\n";
exit(1);
}
this->curLength = ob.curLength;
strcpy(this->ch, ob.ch);
}
#endif
//main.cpp
#include"header.h"
int main()
{
myString st(10), st1("ABCDEFG");
myString st2(st1);
st.print(), st1.print(), st2.print();
st = st1(0, 4);//???
st.print();
return 0;
}
这是一个字符串类,问题出现在了两个符号重载,()和=
()重载是想对字符串对象做一个切片,返回一个临时对象,=重载就不用说了,就是赋值。
问题就出现在总是无法将这个切片后的临时对象赋值给等号前的对象,编译后如下:
在网上一番查找后找到一个类似问题
https://blog.csdn.net/u011068702/article/details/64443949
也就是说,在st1(0,4)时存在了一个临时变量,当把这个临时变量传给st时,由于在=重载函数声明中,参数为myString&,而并不是常量引用。
这个错误是C++编译器的一个关于语义的限制。
如果一个参数是以非const引用传入,c++编译器就有理由认为程序员会在函数中修改这个值,并且这个被修改的引用在函数返回后要发挥作用。但如果你把一个临时变量当作非const引用参数传进来,由于临时变量的特殊性,程序员并不能操作临时变量,而且临时变量随时可能被释放掉,所以,一般说来,修改一个临时变量是毫无意义的,据此,c++编译器加入了临时变量不能作为非const引用的这个语义限制。
了解这个语义以后就简单了,只需给=重载参数加上const常量限制符。
(类中=重载函数声明也别忘了要加上const)
加上以后程序的运行结果
可以,很正确。
总结:
c++中临时变量不能作为非const的引用参数
2019/12/14更新
最近看到一个类似问题,即C++11
int i=0;
++++i;//这样是可以的
i++++;//这样就是错误的
我们知道前++就是直接对对象自增后返回对象,而后++会先返回记录当前值,在自增,最后返回一个无名的临时对象,那么 i++++就是让第一个后++返回的无名临时对象再自增,这样对C++是无意义的,所以这样就无法编译通过。//ps.这就是常说的 举一隅以三隅反 吧
Can not deserialize value of type java.util.Date from String 异常解决办法
添加 webConfigurer 类
类中实现 WebMvcConfigurer 接口中的 extendMessageConverters 接口
1、初始化一个 Jackson2Http 消息转换器类(MappingJackson2HttpMessageConverter)
2、初始化一个 ObjectMapper 对象
3、将 objectMapper 赋值到 mappingJackson2HttpMessageConverter 中的 ObjectMapper 中,如果要自定义格式,可以先使用 ObjectMapper.setDateFormat 方法,自定义日期格式
4、设置中文编码,初始化一个 MediaType 列表 List<MediaType> list = new ArrayList<>()
5、list.add(MediaType.APPLICATION_JSON_UTF8)
6、mappingJave2HttpMessageConverter.setSupportedMediatypes(list)
7、converters.add(mappingJave2HttpMessageConverter)
具体代码如下:
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.ArrayList;
import java.util.List;
/**
* Can not deserialize value of type java.util.Date from String异常解决办法
* @program: ld-zkzx-web
* @description
* @author: ZhangXu
* @create: 2020-03-21 14:28
**/
@Component
public class WebConfigurer implements WebMvcConfigurer {
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
//设置日期格式
ObjectMapper objectMapper = new ObjectMapper();
/*SimpleDateFormat smt = new SimpleDateFormat("yyyy-MM-dd");
objectMapper.setDateFormat(smt);*/
mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);
//设置中文编码格式
List<MediaType> list = new ArrayList<MediaType>();
list.add(MediaType.APPLICATION_JSON_UTF8);
mappingJackson2HttpMessageConverter.setSupportedMediaTypes(list);
converters.add(mappingJackson2HttpMessageConverter);
}
}
cannot convert t (type interface {}) to type string: need type assertion
问题:
在使用interface表示任何类型时,如果要将interface转为某一类型,直接强制转换是不行的,例如:
var t interface{} = "abc" s := string(t)
cannot convert t(type interface {}) to type string: need type assertion
这样是不行的,需要进行type assertion类型断言,具体使用方法请参考:
golang 任何类型interface{}
更多信息:
http://www.jb51.cc/article/p-amhitjiw-bnz.html
cannot convert value of type 'String!' to expected argument type 'inout String'
var area_big:String!var area_small:String!
var types = ""
var firstIn = true
provinces.forEach { (province) in
let count = province.children.count
for i in 0..<count{
if i == 0 && province.children[i].checked {
if area_big == nil {
area_big = province.children[i].attributes["name"]
}else{
area_big += "#" + province.children[i].attributes["name"]!
}
}
}
}
如果是按照上面那样写 ,会报如题错误。改为如下就可以了
var area_big:String! var area_small:String! var types = "" var firstIn = true provinces.forEach { (province) in let count = province.children.count for i in 0..<count{ if i == 0 && province.children[i].checked { if area_big == nil { area_big = province.children[i].attributes["name"] }else{ area_big = area_big + "#" + province.children[i].attributes["name"]! } } } }
关于Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property ...的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于C++之error: cannot bind non-const lvalue reference of type ‘myString&’ to an rvalue of type ‘m...、Can not deserialize value of type java.util.Date from String 异常解决办法、cannot convert t (type interface {}) to type string: need type assertion、cannot convert value of type 'String!' to expected argument type 'inout String'的相关信息,请在本站寻找。
本文标签: