0
我是新來用SpringMVC,今天我寫了DateConverter 這樣爲什麼在類型轉換中發生這種情況?
public class DateConverter implements Converter<String,Date>{
private String formatStr = "";
public DateConverter(String fomatStr) {
this.formatStr = formatStr;
}
public Date convert(String source) {
SimpleDateFormat sdf = null;
Date date = null;
try {
sdf = new SimpleDateFormat(formatStr);
date = sdf.parse(source);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
}
然後我寫這樣
@RequestMapping(value="/converterTest")
public void testConverter(Date date){
System.out.println(date);
}
它congfigure的ApplicationContext控制器,我相信DateConverter已初始化正確的,當我測試我的轉換器
http://localhost:8080/petStore/converterTest?date=2011-02-22
the browser says:
HTTP Status 400 -
type Status report
message
description The request sent by the client was syntactically incorrect().
有人可以幫助我嗎?在此先感謝
如果您更改控制器方法以接收'String'而不是'Date',它是否工作? – acdcjunior
當然這沒關係,但我想要的是將字符串更改爲日期,所以我寫轉換器 – kevin
怎麼樣添加@RequestParam(「日期」)到testConverter(日期日期)? – Hippoom