Spring-MVCのモデルでDate型を使いたい
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
]
開始行:
[[FrontPage]]
#contents
2008/03/01からのアクセス回数 &counter;
* Spring-MVCのモデルでDate型を使いたい [#e05f4c02]
Spring-MVCのAbstractCommandControllerは、デフォルトではDa...
してくれません。
SpringマニュアルのValidation, Data-binding, the BeanWrapp...
PropertyEditorRegistrarsを使ってコントローラのinitBinder...
する方法が紹介されています。
- カスタムプロパティエディタを定義
#pre{{
package com.foo.editors.spring;
public final class CustomPropertyEditorRegistrar implemen...
public void registerCustomEditors(PropertyEditorRegis...
// it is expected that new PropertyEditor instanc...
registry.registerCustomEditor(ExoticType.class, n...
// you could register as many custom property edi...
}
}
}}
- ControllerにinitBinderでカスタムプロパティエディタを登...
#pre{{
public final class RegisterUserController extends SimpleF...
private final PropertyEditorRegistrar customPropertyE...
public RegisterUserController(PropertyEditorRegistrar...
this.customPropertyEditorRegistrar = propertyEdit...
}
protected void initBinder(HttpServletRequest request,...
this.customPropertyEditorRegistrar.registerCustom...
}
// other methods to do with registering a User
}
}}
しかし、この方法だとDate型をモデルに追加したときにすべて...
するしかありません。(継承を使えてばよいと言われるかもし...
Controllerクラスを作成する必要があります)
** Springらしい解決方法 [#kaac7f57]
ソースを辿っていくうちにSimpleFormControllerのpropertyEdi...
カスタムプロパティエディタのRegistrarを追加することで解決...
そこで、CustomPropertyEditorRegistrarクラスを以下のように...
#pre{{
package jp.co.pwv.utils;
import java.beans.PropertyEditor;
import java.util.Iterator;
import java.util.Map;
import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
public class CustomPropertyEditorRegistrar implements Pro...
Map<Class, PropertyEditor> customEditors;
public void registerCustomEditors(PropertyEditorRegistry...
Iterator<Class> itr = customEditors.keySet().iterator();
while (itr.hasNext()) {
Class key = itr.next();
PropertyEditor editor = customEditors.get(key);
registry.registerCustomEditor(key, editor);
}
}
public Map<Class, PropertyEditor> getCustomEditors() {
return customEditors;
}
public void setCustomEditors(Map<Class, PropertyEditor> ...
this.customEditors = customEditors;
}
}
}}
- Spring Bean定義ファイルでCustomPropertyEditorRegistrar...
#pre{{
<bean id="dateFormat" class="java.text.SimpleDateFormat">
<constructor-arg index="0" type="java.lang.String" valu...
</bean>
<bean id="customEditorRegistrar" class="jp.co.pwv.utils....
<property name="customEditors">
<map>
<entry key="java.util.Date">
<bean class="org.springframework.beans.propertye...
<constructor-arg index="0">
<ref bean="dateFormat" />
</constructor-arg>
<constructor-arg index="1" type="boolean" value="tru...
</bean>
</entry>
</map>
</property>
</bean>
}}
ここでは、Springの提供するCustomDateEditorを使ってDate型...
- フォームコントローラのBean定義にpropertyEditorRegistrar...
#pre{{
<property name="propertyEditorRegistrars">
<list>
<ref bean="customEditorRegistrar"/>
</list>
</property>
}}
これでユーザがモデルで定義した型と文字列とを変換すること...
* コメント [#ea96dd56]
この記事は、
#vote(おもしろかった[22],そうでもない[2],わかりずらい[29])
皆様のご意見、ご希望をお待ちしております。
#comment_kcaptcha
終了行:
[[FrontPage]]
#contents
2008/03/01からのアクセス回数 &counter;
* Spring-MVCのモデルでDate型を使いたい [#e05f4c02]
Spring-MVCのAbstractCommandControllerは、デフォルトではDa...
してくれません。
SpringマニュアルのValidation, Data-binding, the BeanWrapp...
PropertyEditorRegistrarsを使ってコントローラのinitBinder...
する方法が紹介されています。
- カスタムプロパティエディタを定義
#pre{{
package com.foo.editors.spring;
public final class CustomPropertyEditorRegistrar implemen...
public void registerCustomEditors(PropertyEditorRegis...
// it is expected that new PropertyEditor instanc...
registry.registerCustomEditor(ExoticType.class, n...
// you could register as many custom property edi...
}
}
}}
- ControllerにinitBinderでカスタムプロパティエディタを登...
#pre{{
public final class RegisterUserController extends SimpleF...
private final PropertyEditorRegistrar customPropertyE...
public RegisterUserController(PropertyEditorRegistrar...
this.customPropertyEditorRegistrar = propertyEdit...
}
protected void initBinder(HttpServletRequest request,...
this.customPropertyEditorRegistrar.registerCustom...
}
// other methods to do with registering a User
}
}}
しかし、この方法だとDate型をモデルに追加したときにすべて...
するしかありません。(継承を使えてばよいと言われるかもし...
Controllerクラスを作成する必要があります)
** Springらしい解決方法 [#kaac7f57]
ソースを辿っていくうちにSimpleFormControllerのpropertyEdi...
カスタムプロパティエディタのRegistrarを追加することで解決...
そこで、CustomPropertyEditorRegistrarクラスを以下のように...
#pre{{
package jp.co.pwv.utils;
import java.beans.PropertyEditor;
import java.util.Iterator;
import java.util.Map;
import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
public class CustomPropertyEditorRegistrar implements Pro...
Map<Class, PropertyEditor> customEditors;
public void registerCustomEditors(PropertyEditorRegistry...
Iterator<Class> itr = customEditors.keySet().iterator();
while (itr.hasNext()) {
Class key = itr.next();
PropertyEditor editor = customEditors.get(key);
registry.registerCustomEditor(key, editor);
}
}
public Map<Class, PropertyEditor> getCustomEditors() {
return customEditors;
}
public void setCustomEditors(Map<Class, PropertyEditor> ...
this.customEditors = customEditors;
}
}
}}
- Spring Bean定義ファイルでCustomPropertyEditorRegistrar...
#pre{{
<bean id="dateFormat" class="java.text.SimpleDateFormat">
<constructor-arg index="0" type="java.lang.String" valu...
</bean>
<bean id="customEditorRegistrar" class="jp.co.pwv.utils....
<property name="customEditors">
<map>
<entry key="java.util.Date">
<bean class="org.springframework.beans.propertye...
<constructor-arg index="0">
<ref bean="dateFormat" />
</constructor-arg>
<constructor-arg index="1" type="boolean" value="tru...
</bean>
</entry>
</map>
</property>
</bean>
}}
ここでは、Springの提供するCustomDateEditorを使ってDate型...
- フォームコントローラのBean定義にpropertyEditorRegistrar...
#pre{{
<property name="propertyEditorRegistrars">
<list>
<ref bean="customEditorRegistrar"/>
</list>
</property>
}}
これでユーザがモデルで定義した型と文字列とを変換すること...
* コメント [#ea96dd56]
この記事は、
#vote(おもしろかった[22],そうでもない[2],わかりずらい[29])
皆様のご意見、ご希望をお待ちしております。
#comment_kcaptcha
ページ名:
SmartDoc