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);
}

将其配置为 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 ().

有人可以帮我吗?提前致谢

4

1 回答 1

1

您的转换器中有错字。您拼错了构造函数参数,因此赋值无效。代替:

public DateConverter(String fomatStr) {
    this.formatStr = formatStr;
}

尝试:

public DateConverter(String formatStr) {
    this.formatStr = formatStr;
}

可能还有其他问题,但至少您需要解决它。我假设您正在使用yyyy-MM-dd您的日期格式?

于 2013-07-29T02:22:02.733 回答