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 に答える