HTTPリクエストで送信されたデータをデータBeanに設定する場合、数値項目に対して不当な文字列を設定すると数値変換エラー(NumberFormatException)となります。 Webcoordinatorでは、デフォルトでは、数値変換エラーは0の入力とみなして処理を継続します。数値変換の厳密な判定や、特別な処理を行うためには、データBeanにUpdateExceptionListenerを実装します。
public class SampleBean extends DataBean implements UpdateExceptionListener { public void handleException(UpdateExceptionEvent uee) { // 例外を知る Exception ex = uee.getException(); // 項目名を知る String propertyName = uee.getName(); // 入力しようとした値を知る Object value = uee.getValue(); // 代替処理の実行 .... // 処理できない場合は再度スローする throw ex; } }
一例として、BigInteger型の項目に対して空文字列が送信された場合にnullを設定する場合、以下のようにします。
// 代替処理の実行 if(propertyName.equals("someBigIntProp") && ex instanceof NumberFormatException) { if(value instanceof String && ((String)value).length() == 0) { setSomeBigIntProp(null); return; } // NumberFormatExceptionの場合はWebcoordinatorが // new BigInteger(0)を設定するので、別の例外に置き換える ex = new IllegalArgumentException("Error in someIntProp"); }