1

アプリケーションで Struts 2 dojo datetimepicker を使用しています。Stringタイプのアクション クラス プロパティ (datetimepicker の値) を SQLに変換してdatetime、値を SQL テーブルに保存するにはどうすればよいですか? 可能であれば、サンプルコードを提供してください。

4

1 に答える 1

2

型コンバーターを作成する必要があります。StrutsTypeConverter を拡張したクラスです。

public class MyConverter extends StrutsTypeConverter{
    //this method converts received object into String
    public String convertToString(Map map, Object object){
        if(object instanceof Date){
            Date date = (Date) object;
            java.text.SimpleDateFormat simpleDateFormat = new java.text.SimpleDateFormat("dd-MM-yyyy");
            return simpleDateFormat.format(date);
        }
        return null;
    }

    //this method convert the received String into a 
    public Object convertFromString(Map map, String[] values, Class clazz) {
        if (values != null && values.length > 0) {
            return parseValue(values[0]);
        }
        return null;
    }
    //this method do the conversion
    public Date parseValue(String value) {
        value = value.trim();
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
            return simpleDateFormat.parse(value);
        } catch (Exception e) {
            return null;
        }
    }
}

コンバーター クラスを作成したら、プロパティ ファイルを作成する必要があります。このファイルの名前は、次のパターンに従う必要があります。

NameOfYourAction-conversion.properties 

これは、アクションがある場所に配置する必要があります。内部には、次のように、変換するフィールドの名前とコンバーターの名前を入力する必要があります。

myfield = com.app.converter.MyConverter

これにより、Struts は自動的に変換を行います。

とにかく、ここにドキュメンテーションがあります。

よろしく。

于 2013-11-04T22:51:39.593 に答える