FrontPage

2008/03/01からのアクセス回数 14424

Spring-MVCのモデルでDate型を使いたい

Spring-MVCのAbstractCommandControllerは、デフォルトではDate型と文字列の変換を してくれません。

SpringマニュアルのValidation, Data-binding, the BeanWrapper, and PropertyEditorsには、 PropertyEditorRegistrarsを使ってコントローラのinitBinderメソッドでCustomEditorsを登録 する方法が紹介されています。

しかし、この方法だとDate型をモデルに追加したときにすべてのControllerでinitBinderを定義 するしかありません。(継承を使えてばよいと言われるかもしれませんが、それでも余計な Controllerクラスを作成する必要があります)

Springらしい解決方法

ソースを辿っていくうちにSimpleFormControllerのpropertyEditorRegistrarsに登録してある カスタムプロパティエディタのRegistrarを追加することで解決することが分かりました。

そこで、CustomPropertyEditorRegistrarクラスを以下のように作成しました。

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 PropertyEditorRegistrar {
	Map<Class, PropertyEditor>	customEditors;
	public void registerCustomEditors(PropertyEditorRegistry registry) {
		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> customEditors) {
		this.customEditors = customEditors;
	}
}
	<bean id="dateFormat" class="java.text.SimpleDateFormat">
		<constructor-arg index="0" type="java.lang.String" value="yyyy-MM-dd" />
	</bean>
	
	<bean id="customEditorRegistrar" class="jp.co.pwv.utils.CustomPropertyEditorRegistrar">
	  <property name="customEditors">
	    <map>
	      <entry key="java.util.Date">
	        <bean class="org.springframework.beans.propertyeditors.CustomDateEditor">
				<constructor-arg index="0">
					<ref bean="dateFormat" />
				</constructor-arg>
				<constructor-arg  index="1" type="boolean" value="true"/>
	        </bean>
	      </entry>
	    </map>
	  </property>
	</bean>

ここでは、Springの提供するCustomDateEditorを使ってDate型の文字列との変換するカスタムエディタを登録しました。

これでユーザがモデルで定義した型と文字列とを変換することができるようになります。

コメント

この記事は、

選択肢 投票
おもしろかった 7  
そうでもない 0  
わかりずらい 1  

皆様のご意見、ご希望をお待ちしております。


(Input image string)

トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
SmartDoc