FrontPage > Spring Web MVCの覚書
フィールドフォーマット †
リクエストパラメータからJavaBeanフィールドへ設定、およびフィールド取得時のフォーマットを指定。
1. アノテーションによるフォーマット †
サーブレット設定ファイル(servlet-context.xml等)に以下を記述。
<mvc:annotation-driven />
1.1 @NumberFormat †
属性:
style=NumberFormat.Style
pattern="パターン"
例)
public class SampleCommand {
@NumberFormat(style=Style.NUMBER)
private BigDecimal balance;
・・・略
1.2 @DateTimeFormat †
属性:
style="スタイルパターン"
iso=DateTimeFormat.ISO
pattern="パターン"
例)
public class SampleCommand {
@DateTimeFormat(iso=ISO.DATE)
private Date date;
・・・略
2. バインダーのカスタマイズによるフォーマット †
- コントローラに@InitBinderアノテーションを付与したメソッドを作成する。
例)
・・・略
@Controller
public class SampleController {
@InitBinder
public void initBinder(WebDataBinder binder) {
}
・・・略
- メソッド内で必要なPropertyEditorを生成し、バインダにセットする。
例) 日付のフォーマットを指定する。
@InitBinder
public void initBinder(WebDataBinder binder) {
// フォーマット作成
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
format.setLenient(false);
// sampleDateフィールドのフォーマットを指定(null可)
binder.registerCustomEditor(Date.class, "sampleDate", new CustomDateEditor(format, true));
}
例) Stringのフィールドに対して空文字をnullとしてバインドする。
@InitBinder
public void initBinder(WebDataBinder binder) {
// Stringクラスのフィールドに対して適用
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
}
例) 指定したフィールドのみバインドする。
@InitBinder
public void initBinder(WebDataBinder binder) {
// sampleで始まるフィールドのみバインドする。(複数指定可)
binder.setAllowedFields("sample*");
}
2.1 PropertyEditor一覧 †
- ByteArrayPropertyEditor
byte arraysへ変換。
- ClassEditor
クラス名からクラスへ変換。
- CustomBooleanEditor
Booleanへ変換。
- CustomCollectionEditor
Collectionへ変換。
- CustomDateEditor
java.util.Dateへ変換。
- CustomNumberEditor
Numberのsubclassへ変換。
- FileEditor
java.io.Fileへ変換。
- InputStreamEditor
一方向のproperty editor。InputStreamへ変換。 closeはされない。
- LocaleEditor
Localeへ変換。[language]_[country]_[variant]
- PatternEditor
JDK 1.5 Patternへ変換。
- PropertiesEditor
Propertiesへ変換。
- StringTrimmerEditor
空文字をnullへ変換する。デフォルトでは登録されていない。
- URLEditor
URLへ変換。
3. WebBindingInitializerによるカスタマイズ †
- WebBindingInitializerインターフェースを実装したクラスを作成する。
initBinderメソッドの内容は@InitBinderの場合と同じ。
例)
package org.springframework.samples.mvc.basic.sample;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.context.request.WebRequest;
public class SampleBindingInitializer implements WebBindingInitializer {
public void initBinder(WebDataBinder binder, WebRequest req) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
format.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(format, true));
}
}
- 上記クラスを登録するために、Servlet設定ファイル(servlet-context.xml等)へ以下を記述する。
・・・略
<!-- Scans the classpath of this application for @Components to deploy as beans -->
<context:component-scan base-package="org.springframework.samples.mvc.basic" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="cacheSeconds" value="0" />
<property name="webBindingInitializer">
<bean class="org.springframework.samples.mvc.basic.sample.SampleBindingInitializer" />
</property>
</bean>
・・・略
- この方法ではアノテーションによるフォーマットは無効になる。また上記例ではValidatorを指定していないためvalidationは行われない。
アノテーションによるValidationを有効にするには下記のようにする。
public class SampleBindingInitializer implements WebBindingInitializer {
@Autowired
private Validator validator;
public void initBinder(WebDataBinder binder, WebRequest req) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
format.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(format, true));
binder.setValidator(validator);
}
}
4. エラーメッセージ †